C++ 使用堆栈存储对象
我正在做一个程序,我需要在堆栈中存储对象。我将堆栈定义为一个模板类,如下所示:C++ 使用堆栈存储对象,c++,stack,C++,Stack,我正在做一个程序,我需要在堆栈中存储对象。我将堆栈定义为一个模板类,如下所示: template < class T > class stackType { private: int maxStackSize; int stackTop; T *list; // pointer to the array that holds the stack elements public: stackType( int stackSize );
template < class T >
class stackType
{
private:
int maxStackSize;
int stackTop;
T *list; // pointer to the array that holds the stack elements
public:
stackType( int stackSize ); // constructor
~stackType(); // destructor
void initializeStack();
bool isEmptyStack();
bool isFullStack();
void push( T newItem );
T top();
void pop();
};
// class function
Matrix_AR() {}
Matrix_AR( int m, int n ); // initialize the matrix of size m x n
void inputData( string fileName ); // read data from text file
void display(); //...
但是,当我声明这样的函数时
void myfunction( stackType<Matrix_AR>& stack )
{
Matrix_AR item1, item2, item3;
stack.push( item1 );
stack.push( item2 );
stack.push( item3 );
}
void myfunction(堆栈类型和堆栈)
{
矩阵_AR第1项、第2项、第3项;
堆栈推送(第1项);
堆栈推送(第2项);
堆栈推送(第3项);
}
我不断地犯错误。我试了五个小时才修好,但还是弄不明白。有人能帮忙吗
Undefined symbols for architecture x86_64:
"Matrix_AR::Matrix_AR()", referenced from:
myfunction(stackType<Matrix_AR>&, char&, bool&)in main.o
ld: symbol(s) not found for architecture x86_64
架构x86_64的未定义符号:
“Matrix_AR::Matrix_AR()”,引用自:
main.o中的myfunction(stackType&,char&,bool&)
ld:找不到架构x86_64的符号
您似乎没有定义默认构造函数。如果您想要快速解决方案,只需如下声明:
template < class T >
class stackType
{
private:
int maxStackSize;
int stackTop;
T *list; // pointer to the array that holds the stack elements
public:
stackType( int stackSize ); // constructor
~stackType(); // destructor
void initializeStack();
bool isEmptyStack();
bool isFullStack();
void push( T newItem );
T top();
void pop();
};
// class function
Matrix_AR() {}
Matrix_AR( int m, int n ); // initialize the matrix of size m x n
void inputData( string fileName ); // read data from text file
void display(); //...
发布的错误是针对默认构造函数的,但是如果您没有定义其他函数,那么这些函数也会出现类似的错误。您应该有一个单独的.cpp文件,其中包含所有成员函数的定义
Matrix_AR::Matrix_AR()
{
...
}
Matrix_AR::Matrix_AR( int m, int n )
{
...
}
void Matrix_AR::inputData( string fileName )
{
...
}
etc.......
有什么特别的原因不使用吗?哦,是的,你修好了。非常感谢:D.我觉得我现在太笨了