c2955 Error - Use of Class template reuires argume

2019-03-02 05:06发布

问题:

So, I've tested vector and it seems to be running fine. However, I'm trying to implement a basic Stack class built off of my Vector class. 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:

#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:

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:

You have defined Vector<T> as a member of Stack already, it's not necessary to inherit from 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:

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();}

};