书下一个习题,看了答案答案也是错的,求大神指点

书上一个习题,看了答案答案也是错的,求大神指点
本帖最后由 wsfxzxb 于 2013-01-10 14:33:04 编辑
编写并调用函数模板,在保存未知类型对象的 vector 中查找中间值。(注:中间值是这样一个值,一半元素比它大,一半元素比它小。)用常规方式构造你的程序:函数定义应放在一个文件中,它的声明放在一个头文件中,定义和使用函数模板的代码应包含该头文件。

我的问题:怎么分离定义?这是《C++Primer》第四版中第16章的一个题。配套习题答案书的解答居然是错的,我用vs2005无法编译通过。

------解决方案--------------------
引用:
怎么分离定义?有些编译器还不支持呢,所以尽量使用包含方式定义

export已经被废弃了
------解决方案--------------------

//先声明编译器几乎不支持分离
#ifndef QUEUE_H
#define QUEUE_H
#include <iostream>
 
template <class Type> class Queue;
template <class T> 
std::ostream& operator<<(std::ostream&, const Queue<T>&);
template <class Type> class QueueItem
{
    friend class Queue<Type>;
    friend std::ostream& 
    operator<< <Type> (std::ostream&, const Queue<Type>&);
    QueueItem(const Type &t): item(t), next(0) {}
    Type item;
    QueueItem *next;
};
 
template <class Type> class Queue
{
    friend std::ostream& 
    operator<< <Type> (std::ostream&, const Queue<Type>&);
public:
    Queue(): head(0), tail(0) {}
public:
    template <class It> 
    Queue(It beg, It end): 
          head(0), tail(0) { copy_elems(beg, end); }
    Queue(const Queue &Q): head(0), tail(0)
                { copy_elems(Q); }
    Queue& operator=(const Queue&);
    ~Queue() { destroy(); }
    template <class Iter> void assign(Iter, Iter);
    Type& front() { return head->item; }
    const Type& front() const { return head->item; }
    void push (const Type &);
    void pop();
    bool empty() const { return head == 0; }
private:
    QueueItem<Type> *head;
    QueueItem<Type> *tail;
    void destroy();
    void copy_elems(const Queue&);
private:
    template <class Iter> void copy_elems(Iter, Iter);
};
#include "Queue.cpp"
#endif
/////////////////////////////////////////////
#ifndef QUEUE_CPP
#define QUEUE_CPP
template <class Type> 
void Queue<Type>::pop()
{
    QueueItem<Type>* p = head;
    head = head->next;
    delete p;