0

Basically all I want to do is define a generic stack using arrays in stack-array.cpp, and use it in stack-test.cpp.

I've read a few answers here and here but somehow i'm still unable to create a stackarray in stack-test.cpp. I'm a C++ beginner and I'm afraid I'm at my wits end..

Here is the code:

stack-array.h

template<typename T>
class stackarray {
public:
    int stacksize;
    T stacks;
    T *sp;

    T pop();
    void push(T);
    int isempty();
    stackarray<T>();
    virtual ~stackarray<T>();
};

stack-array.cpp

#include "stack-array.h"
#include <stdio.h>
#include <string.h>
#include <stdlib.h>

    template<typename T>
    stackarray<T>::stackarray()
    {
        stackarray<T>::stacksize =10;
        stackarray<T>::stacks = new T[stacksize];
        stackarray<T>::sp = stacks;
    }

    template<typename T>
    stackarray<T>::~stackarray()
    {
        // TODO Auto-generated destructor stub
    }

    template<typename T>
    void push(T n)
    {
        // TODO Auto-generated destructor stub
    }

    template<typename T>
    T pop()
    {
        // TODO Auto-generated destructor stub
    }

    template<typename T>
    int isempty()
    {
        // TODO Auto-generated destructor stub
    }

    template<typename T>
    T applyToAll() {
        // TODO Auto-generated destructor stub
    }

stack-test.cpp

# include <iostream>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>

# include "stack-array.h"


int main() {
    stackarray<int> x; //erroring here

    return 0;
}

this gives me several "undefined references to stackarray :: stackarray()"

Community
  • 1
  • 1
Gwen Wong
  • 357
  • 3
  • 15
  • Your second link answers the question. You'll need to define the template (including all its member functions) in the header, not in a source file. – Mike Seymour Mar 16 '15 at 16:50
  • I'm not sure i understand...surely thats what I did? Did you mean removing the template things from stack-array.cpp? It then seizes to stop working? – Gwen Wong Mar 16 '15 at 17:27
  • Yes, all the function definitions will need to be in the header, as the other question describes. You can't define a template (including a member of a class template) in one source file and use it from another. – Mike Seymour Mar 16 '15 at 17:28

0 Answers0