使用堆栈来存储对象

时间:2012-10-21 01:02:03

标签: 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 );  // constructor
    ~stackType();                // destructor
    void initializeStack();
    bool isEmptyStack();
    bool isFullStack();
    void push( T newItem );
    T top();
    void pop();
};

这是我的班级:

class Matrix_AR
{
public:
    float **Matrix;        // Matrix as a pointer to pointer
    int rows, columns;     // number of rows and columns of the matrix

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

但是,当我声明一个像这样的函数时

void myfunction( stackType<Matrix_AR>& stack )
{
    Matrix_AR item1, item2, item3;

    stack.push( item1 );
    stack.push( item2 );
    stack.push( item3 );
}

我一直收到错误。我试图将它修复五个小时仍然无法弄明白。请有人帮忙!

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

1 个答案:

答案 0 :(得分:2)

好像你没有定义你的默认构造函数。如果你想要一个快速的解决方案,只需声明如下:

// 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.......