"Array.h"template <class elemType>
class Array
{
public:
explicit Array (int size=DdfaultArraySize);
Array (elemType *array,int array_size);
Array (const Array &rhs); virtual ~Array(){delete [] ia;} bool operator==(const Array&)const;
bool operator!=(const Array&)const;
Array& operator=(const Array&);
int size()const{return _size;} virtual elemType& operator [] (int index){return ia[index];}
virtual void sort(); virtual elemType min() const;
virtual elemType max() const;
virtual int  find(const elemType &value)const;
protected:
        static const int DdfaultArraySize=12;
int _size;
elemType *ia;
};#include<iostream>
#include"Array.h"
using namespace std;
int main()
{
const int array_size=4;
Array<int> ia(array_size);
Array<double> da(array_size);
Array<char> ca(array_size);

int ix;
for (ix=0;ix<array_size;++ix)
{
ia[ix]=ix;
da[ix]=ix*1.75;
ca[ix]=ix+'a';
}
for (ix=0;ix=array_size;++ix)
{
cout<<"["<<ix<<"]ia:"<<ia[ix]
<<"\tca:"<<ca[ix]
<<"\tca:"<<da[ix]<<endl;
}
return 0;
}
--------------------Configuration: template - Win32 Debug--------------------
Compiling...
template.cpp
g:\c++ primer code\template\array.h(23) : error C2258: illegal pure syntax, must be '= 0'
        g:\c++ primer code\template\array.h(26) : see reference to class template instantiation 'Array<elemType>' being compiled
g:\c++ primer code\template\array.h(23) : error C2252: 'DdfaultArraySize' : pure specifier can only be specified for functions
        g:\c++ primer code\template\array.h(26) : see reference to class template instantiation 'Array<elemType>' being compiled
g:\c++ primer code\template\array.h(23) : error C2258: illegal pure syntax, must be '= 0'
        g:\c++ primer code\template\template.cpp(7) : see reference to class template instantiation 'Array<int>' being compiled
g:\c++ primer code\template\array.h(23) : error C2252: 'DdfaultArraySize' : pure specifier can only be specified for functions
        g:\c++ primer code\template\template.cpp(7) : see reference to class template instantiation 'Array<int>' being compiled
g:\c++ primer code\template\array.h(23) : error C2258: illegal pure syntax, must be '= 0'
        g:\c++ primer code\template\template.cpp(8) : see reference to class template instantiation 'Array<double>' being compiled
g:\c++ primer code\template\array.h(23) : error C2252: 'DdfaultArraySize' : pure specifier can only be specified for functions
        g:\c++ primer code\template\template.cpp(8) : see reference to class template instantiation 'Array<double>' being compiled
g:\c++ primer code\template\array.h(23) : error C2258: illegal pure syntax, must be '= 0'
        g:\c++ primer code\template\template.cpp(9) : see reference to class template instantiation 'Array<char>' being compiled
g:\c++ primer code\template\array.h(23) : error C2252: 'DdfaultArraySize' : pure specifier can only be specified for functions
        g:\c++ primer code\template\template.cpp(9) : see reference to class template instantiation 'Array<char>' being compiled
Error executing cl.exe.template.exe - 8 error(s), 0 warning(s)

解决方案 »

  1.   

    你的Array类的成员函数实现了没有?
      

  2.   

    你的实现文件呢!
    下面是我自己写的实现!我没看过这本书---不知书上用什么方法实现的,应该很简单的
    //file:array.h
    template <class elemType>
    class Array
    {
    public:
      explicit Array (int size=DdfaultArraySize)
      {
    _size=size;
    ia=new elemType[size];
      }
      Array (elemType *array,int array_size)
      {
    ia=new elemType[array_size];
    for(int i=0;i<array_size;i++)
      ia[i]=array[i];
    _size=array_size;
      }
    Array (const Array &rhs)
    {
      ia= new elemType[rhs._tsize];
      for(int i=0;i<array_size;i++)
     ia[i]=array[i];
      _size=rhs._size;
    }
    virtual ~Array(){delete [] ia;}
    bool operator==(const Array &rhs)const
    {
      bool b_flag=true;
      if(_size!=rhs._size)
    return false;
      else
      {
    for(int i=0;i<_size;i++)
      if(ia[i]!=rhs[i])
    return false;
      }
      return true;
    }
    bool operator!=(const Array&rhs)const
    {
      return !(*this==rhs);
    }
    Array& operator=(const Array&)
    {
      delete [] ai;
       ia= new elemType[rhs._tsize];
        for(int i=0;i<array_size;i++)
     ia[i]=array[i];
    _size=rhs._size;
    }
    int size()const
    {
      return _size;
    }
    virtual elemType& operator [] (int index)
    {
      return ia[index];
    }
    virtual void sort();
    virtual elemType min() const
    {
      elemType temp;
      temp=ia[0];
      for(int i=1;i<_size;i++)
    if(ia[i]<temp)
      temp=ia[i];
    return temp;
    }
    virtual elemType max() const
    {
      elemType temp;
      temp=ia[0];
      for(int i=1;i<_size;i++)
    if(ia[i]>temp)
      temp=ia[i];
    return temp;
    }
    virtual int  find(const elemType &value)const
    {
      for(int i=0;i<_size;i++)
    if(ia[i]==value)
      return i;
    return -1;
    }
    protected:
      //不能再类里面给静态变量赋值,静态变量实际上是全局变量,应在类外面赋值
            static  const int DdfaultArraySize;
    int _size;
    elemType *ia;
    };
    //初始化静态变量
     template<class elemType>
     const  int
     Array<elemType>::DdfaultArraySize=12;template<class type>
    void Array<type>::sort()
    {
      type temp;
      for(int i=0;i<_size-1;i++)
    for(int j=0;j<_size-1-i;j++)
      if(ia[j]>ia[j+1])//升序
      {
    temp=ia[j+1];
    ia[j+1]=ia[j];
    ia[j]=temp;
      }
    }//file :array.cpp
    #include<iostream>
    #include"Array.h"
    using namespace std;
    int main()
    {
    const int array_size=4;
    Array<int> ia(array_size);
    Array<double> da(array_size);
    Array<char> ca(array_size);

    int ix;
    for (ix=0;ix<array_size;++ix)
    {
    ia[ix]=ix;
    da[ix]=ix*1.75;
    ca[ix]=ix+'a';
    }
    for (ix=0;ix<array_size;++ix)
    {
    cout<<"["<<ix<<"]ia:"<<ia[ix]
    <<"\tca:"<<ca[ix]
    <<"\tca:"<<da[ix]<<endl;
    }
    cout<<"The max is :"<<ia.max()<<" in array ia "<<endl;
    cout<<"The max is : "<<da.max()<<" in array da"<<endl;
    if(ia.find(3)!=-1)
      cout<<"I have found the value in array ia"<<endl;
    return 0;
    }
      

  3.   

    [0]ia:0 ca:a    ca:0
    [1]ia:1 ca:b    ca:1.75
    [2]ia:2 ca:c    ca:3.5
    [3]ia:3 ca:d    ca:5.25
    The max is :3 in array ia
    The max is : 5.25 in array da
    I have found the value in array ia
    Press any key to continue---------------
    上面是屏幕显示!