简体   繁体   English

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

[英]c2955 Error - Use of Class template reuires argument list

So, I've tested vector and it seems to be running fine. 因此,我已经测试了vector,它似乎运行良好。 However, I'm trying to implement a basic Stack class built off of my Vector class. 但是,我试图实现基于Vector类构建的基本Stack类。 I keep running into these errors when I go to build: 在构建时,我不断遇到这些错误:

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

followed by: 其次是:

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

Here is the Vector.h file: 这是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;
}

and stack.h: 和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()};

};

You have defined Vector<T> as a member of Stack already, it's not necessary to inherit from Vector 您已经将Vector<T>定义为Stack的成员,没有必要从Vector继承

Update 更新资料

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

To: 至:

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

Or if you want to inherit from a template class, you need to provide template parameter 或者,如果您想从模板类继承,则需要提供模板参数

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

Note: 注意:

You miss a few semicolons in Stack function definition and a bug in Stack::push_back as well, I may suggest update to below: 您可能会错过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();}

};

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM