2013-09-29 44 views
1

所以,我測試向量,它似乎運行良好。不過,我想實現建造了我的Vector類的基本Stack類。我一直運行到這些錯誤,當我去建:c2955錯誤 - 類模板的使用reuires參數列表

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

}; 

回答

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

}; 
相關問題