c2955错误 - 使用类模板需要参数列表

时间:2013-09-29 02:00:59

标签: c++ templates vector stack

所以,我测试了矢量,它似乎运行良好。但是,我试图实现一个基于我的Vector类构建的基本Stack类。我去构建时一直遇到这些错误:

stack.h(4): error C2955: 'Vector' : use of class template requires template argument list

接下来是:

vector.h(11) : see declaration of 'Vector'
stack.h(13) : see reference to class template instantiation 'Stack<T>' being compiled

这是Vector.h文件:

#include<iostream>
using namespace std;

const int SIZEFACTOR = 4;

template <class T>
class Vector{
private:
    unsigned int size_Of_Vector; // # of Items in list
    unsigned int total_Vector_Capacity;//Total Capacity
    T * vector_array;//Items themselves


public:
    Vector();
    ~Vector();
    void push_back(const T &e);
    void pop_back();
    bool empty();
    int size() const;
    void growVector();
    void shrinkVector();
    void shrinkToSize();
    int currentCapacity() const;

    //Operator
    const T & operator [] (int index){

        if((index >= size_Of_Vector) || index < 0){
            cout << "ERROR! Index not used: " << index
                 << " (max = " << size_Of_Vector << ")" << endl;
            return EXIT_FAILURE;            
        }    

        return vector_array[index]; 
    };//End Operator    
};//End Header Definition


template <class T> 
Vector<T>::Vector(){//Constructor
    total_Vector_Capacity = 2;//Initially two items
    size_Of_Vector = 0;//Nothing is in the vector yet
    vector_array = new T [total_Vector_Capacity];//Initially two items
}
template <class T>
Vector<T>::~Vector (){//Destructor
         total_Vector_Capacity = 0;
         size_Of_Vector = 0;     
         delete [] vector_array;         
}
template <class T> 
void Vector<T>::growVector(){
    total_Vector_Capacity = total_Vector_Capacity * SIZEFACTOR; //Quarter Size

    //Temp Array
    T * temp_array;
    temp_array = new T [total_Vector_Capacity];
    //Copy
    for(unsigned int i = 0; i < size_Of_Vector; i++){
        temp_array[i] = vector_array[i];
    }
    //Delete old array
    delete [] vector_array;
    //Re-initilize main array
    vector_array = new T [total_Vector_Capacity];
    //Copy old Data back to original array
    for(unsigned int i = 0; i < size_Of_Vector; i++){
        vector_array[i] = temp_array[i];
    }
    //Delete temp array
    delete [] temp_array;
}
template <class T> 
void Vector<T>::shrinkVector(){
    total_Vector_Capacity = (int) (total_Vector_Capacity / SIZEFACTOR); //Quarter Size

    //Temp Array
    T * temp_array;
    temp_array = new T [total_Vector_Capacity];
    //Copy
    for(unsigned int i = 0; i < size_Of_Vector; i++){
        temp_array[i] = vector_array[i];
    }
    //Delete old array
    delete [] vector_array;
    //Re-initilize main array
    vector_array = new T [total_Vector_Capacity];
    //Copy old Data back to original array
    for(unsigned int i = 0; i < size_Of_Vector; i++){
        vector_array[i] = temp_array[i];
    }
    //Delete temp array
    delete [] temp_array;
}
template <class T> 
void Vector<T>::shrinkToSize(){
    total_Vector_Capacity = size_Of_Vector; //Quarter Size

    //Temp Array
    T * temp_array;
    temp_array = new T [total_Vector_Capacity];
    //Copy
    for(unsigned int i = 0; i < size_Of_Vector; i++){
        temp_array[i] = vector_array[i];
    }
    //Delete old array
    delete [] vector_array;
    //Re-initilize main array
    vector_array = new T [total_Vector_Capacity];
    //Copy old Data back to original array
    for(unsigned int i = 0; i < size_Of_Vector; i++){
        vector_array[i] = temp_array[i];
    }
    //Delete temp array
    delete [] temp_array;
}
template <class T> 
void Vector<T>::push_back(const T &e){
    if(size_Of_Vector == total_Vector_Capacity){//Resize if size equals capacity
        cout << "\nGrow now\n";
        growVector();
    }
    vector_array[size_Of_Vector]=e;

    size_Of_Vector++;//Increase Vector Size
}
template <class T> 
void Vector<T>::pop_back(){
    if(size_Of_Vector == (int)(total_Vector_Capacity/SIZEFACTOR)){//Resize if size equals capacity
    cout << "\nShrink now\n";
    shrinkVector();
    }   
    size_Of_Vector--;//Increase Vector Size
}
template <class T>
bool Vector<T>::empty(){
    if(size_Of_Vector==0){
        return true;
    }
    else{
        return false;
    }
}
template <class T>
int Vector<T>::size() const{
    return size_Of_Vector;
}
template <class T>
int Vector<T>::currentCapacity() const{
    return total_Vector_Capacity;
}

和stack.h:

template <class T>
class Stack : public Vector{
private:
    Vector<T> stack;
public:
    Stack(){};
    void push(T x) {stack.push_back(&x)};
    void pop(){stack.pop_back()};
    bool empty(){stack.empty()};

};

1 个答案:

答案 0 :(得分:2)

您已将Vector<T>定义为Stack的成员,没有必要继承Vector

更新

template <class T>
class Stack : public Vector{       
   //...
};

要:

template <class T>
class Stack {
   Vector<T> stack;   
   //...
};

或者,如果要从模板类继承,则需要提供模板参数

template <class T>
class Stack : public Vector<T>{
//                         ^^^

注意:

您错过了Stack函数定义中的几个分号以及Stack::push_back中的错误,我建议更新到以下内容:

template <class T>
class Stack 
{
private:
    Vector<T> stack;
public:
    Stack(){};
    void push(T const& x) {stack.push_back(x);}
    void pop(){stack.pop_back();}
    bool empty(){return stack.empty();}

};