Harry
Harry

Reputation: 95

Using stack to store objects

I'm doing a program where I need to store objects in stack. I define the stack as a template class like this:

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

And this is my class:

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

However, when I declare a function like this

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

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

I kept getting the error. I tried to fix it for five hours and still cannot figure it out. Could anyone help, please !!!

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

Upvotes: 0

Views: 273

Answers (1)

imreal
imreal

Reputation: 10378

It seems like you are not defining your default constructor. If you want a quick solution just declare it like this:

// 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(); //...

The error posted is for the default constructor, but if you are not defining the other functions, you are going to get similar errors for those too. You should have a separate .cpp file with the definitions of all your member functions.

Matrix_AR::Matrix_AR()
{
...
}

Matrix_AR::Matrix_AR( int m, int n )
{
...
}

void Matrix_AR::inputData( string fileName )
{
...
}

etc.......

Upvotes: 2

Related Questions