问题描述
所以,我已经测试了矢量,它似乎运行良好.但是,我正在尝试实现一个基于我的 Vector 类的基本 Stack 类.我在构建时不断遇到这些错误:
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
后跟:
vector.h(11) : see declaration of 'Vector'
stack.h(13) : see reference to class template instantiation 'Stack<T>' being compiled
这里是 Vector.h 文件:
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 << "
Grow now
";
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 << "
Shrink now
";
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()};
};
推荐答案
你已经将Vector
定义为Stack
的成员,不需要继承来自矢量
You have defined Vector<T>
as a member of Stack
already, it's not necessary to inherit from Vector
更新
template <class T>
class Stack : public Vector{
//...
};
收件人:
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>{
// ^^^
注意:
你错过了 Stack
函数定义中的几个分号和 Stack::push_back
中的一个错误,我可能会建议更新到以下内容:
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();}
};
这篇关于c2955 错误 - 使用类模板需要参数列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!