三个源代码文件:
*************
LinearList.h
**************
#include <iostream.h>
template<class T>
class LinearList
{
friend ostream & operator<<(ostream &out,const LinearList<T> &x);//重载<<
     //使用成员函数重载时,最左边的操作数必须是当前类的对象。如果不能,则
     //必须使用友元函数重载
     
public:
LinearList(int MaxListSize=10);
LinearList(LinearList &l);
~LinearList() {delete []element;} bool IsEmpty() const {return length==0;}//判断线性表是否为空
int Length() const {return length;}     //得到线性表的长度
bool Find(int k,T &x) const;            //得到第k个元素,并存入x中 
int Search(const T &x) const;           //在表中查找是否有x,并返回所在位置
LinearList<T> & Delete(int k, T &x);    //得到第k个元素,并存入x中,再删除之
LinearList<T> &Insert(int k, const T &x);//在第k个元素后插入x
  
  private:
int length; //表实长
int MaxSize;//表总长
T *element; //数组指针
};#endif***********
LinearList.cpp
***********
#include "LinearList.h"
#include <iostream.h>template<class T>
LinearList<T>::LinearList(int MaxListSize) 
{
MaxSize=MaxListSize;
element=new T[MaxSize];
length=0;
}template<class T>
LinearList<T>::LinearList(LinearList &s)
{
MaxSize=s.MaxSize;
length=s.length;
//if(element) delete []element;
element=new T[MaxSize];
for(int i=0;i<length;i++)
element[i]=s.element[i];
}template<class T> 
bool LinearList<T>::Find(int k, T&x) const
{
if(k<1 || k>length) return false;
x=element[k-1];
return true;
}template<class T>
int LinearList<T>::Search(const T&x) const
{
for(int i=0;i<length;i++)
if(element[i]==x) return ++i;
  return 0;
}template<class T>
LinearList<T> & LinearList<T>::Delete(int k, T&x)
{
if(Find(k,x))
{
for(int i=k;i<length;i++)
element[i-1]=element[i];
length--;
  return *this;
}
//else throw OutOfBounds();
}template<class T>
LinearList<T> & LinearList<T>::Insert(int k,const T&x)
{
for(int i=length-1;i>=k;i--)
element[i+1]=element[i];
element[k]=x;
length++; return *this;
}template<class T>
ostream & operator<<(ostream &out,const LinearList<T> &x)
{
for(int i=0;i<length;i++)
out<<x.element[i]<<" ";
return out;
}*************
test.cpp
*************
#include <iostream.h>
#include "LinearList.h"void main()
{
  LinearList<int> L(5);
cout<<"Length="<<L.Length()<<endl;
cout<<"IsEmpty="<<L.IsEmpty()<<endl; L.Insert(0,2).Insert(1,6); cout<<"List is:"<<L<<endl;
cout<<"IsEmpty="<<L.IsEmpty()<<endl; cout<<"hello world!\n";
}哪为高手能指点迷津?十分感谢!

解决方案 »

  1.   

    LinearList.h文件缺少
    #ifndef _base_h_
    #define _base_h_
    因为后边只有#endif
    还有,把模板函数的实现跟声明都放到头文件中吧
      

  2.   

    如楼上所说,模板的声明和实现都需要在.h文件中,VC中需要这样
      

  3.   

    不要用win32控制台
    用命令控制抬3
      

  4.   

    感谢各位,尤其是aspvbjava(注定走入地狱)和mscf(扎西特勒)
      

  5.   

    模板申明怎么跑到.cpp中了??这样肯定不行的!!