2012-02-19 7 views
2

나는 다음과 같은 코드가 있습니다 C++ 추상 클래스 템플릿

#define MAXSIZE 50 
template <class T> 
class ListArray : public ListBase 
{//for now to keep things simple use int type only later upgrade to template 
private: 
    T arraydata[MAXSIZE]; 
public: 

    bool insert(int index,const T &item) 
    { 
     if(index >= MAXSIZE) 
      return false;//max size reach 
     if (index<0 || index > getSize())//index greater than array size? 
     { 
      cout<<"Invalid index location to insert"<<endl; 
      return false;//invalid location 
     } 

     for(int i = getSize()-1 ; i >= index;i--) 
     {//shift to the right 
      arraydata[i+1]=arraydata[i]; 
     } 
     arraydata[index] = item; 
     _size++; 
     return true; 
    } 

    string ListArray::toString() 
    { 
     ostringstream ostr; 
     ostr<<"["; 
     for(int i = 0; i < _size;i++) 
      ostr<<arraydata[i]<<' '; 
     ostr<<"]"<<endl; 
     return ostr.str(); 
    } 
}; 

내 MAIN.CPP : 내 두 번째 파일은 서브 클래스를 정의

template <typename T> 
class ListBase 
{ 
protected: 
    int _size; 
public: 
    ListBase() {_size=0;} 
    virtual ~ListBase() {} 
    bool isEmpty() {return (_size ==0);} 
    int getSize() {return _size;} 

    virtual bool insert(int index, const T &item) = 0; 
    virtual bool remove(int index) = 0; 
    virtual bool retrieve(int index, T &item) = 0; 
    virtual string toString() = 0; 
}; 

int main() 
{ 
    ListArray<char> myarray; 
    myarray.insert(0,'g'); 
    myarray.insert(0,'e'); 
    myarray.insert(1,'q'); 
    cout<<myarray.toString(); 
} 

그럴 수 없어 하위 클래스가있는 템플릿을 사용하는 방법을 알아내는 것 같습니다.

error C2955: 'ListBase' : use of class template requires template argument list see reference to class template instantiation 'ListArray' being compiled

답변

6
class ListArray : public ListBase 

class ListArray : public ListBase<T> 

해야합니다 그리고 당신은 기본 클래스 멤버 접근에 문제가 잔뜩있어 : 내 코드를 컴파일 할 때, 나는 다음과 같은 오류가 발생합니다. 참조 : Accessing inherited variable from templated parent class.

9

ListBase에 대한 템플리트 매개 변수를 지정하지 않았습니다.

template <class T> 
    class ListArray : public ListBase<T> 
            --- 
+0

오하이오. 귀하의 권리입니다. 감사합니다. – user1203499