Extremely basic question( undefined ref )
Dec 7, 2019 at 5:52pm UTC
So I thought I could solve this pretty trivial problem myself but to the contrary.
I have a Stack header and a Stack source file containing the definitions
but I'm getting an undefined reference to both the Stacks constructor and destructor, I'm not sure why because I have defined both the constructor and destructor in the cpp file and also declared them in the header.
Stack.cpp
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55
#include "Stack.h"
template <class T>
Stack<T>::Stack()
{
CAPACITY = 1000;
object = new T[CAPACITY];
s = -1;
}
template <class T>
Stack<T>::Stack(int cap)
{
CAPACITY = cap;
object = new T[CAPACITY];
s = -1;
}
template <class T>
bool Stack<T>::isEmpty()
{
return (s < 0);
}
template <class T>
T& Stack<T>::top()
{
return object[s];
}
template <class T>
T Stack<T>::pop()
{
return object[s--];
}
template <class T>
void Stack<T>::push(T obj)
{
++s;
object[s] = obj;
}
template <class T>
int Stack<T>::size()
{
return s + 1;
}
template <class T>
Stack<T>::~Stack()
{
delete [] object;
}
Stack.h
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26
#ifndef STACK_H
#define STACK_H
template <class T>
class Stack
{
public :
Stack();
Stack(int cap);
bool isEmpty();
T& top();
T pop();
void push(T);
int size();
~Stack();
protected :
private :
int s;
T* object;
const int CAPACITY;
};
#endif // STACK_H
Main.cpp
1 2 3 4 5 6 7 8 9 10
#include "Stack.h"
#include <iostream>
int main()
{
Stack<int > intStack;
}
\C++\StackDataStructure\main.cpp|9|undefined reference to `Stack<int>::Stack()'|
Dec 7, 2019 at 5:55pm UTC
You aren't building and/or linking the Stack.cpp file
What are you using to build your program?
Dec 7, 2019 at 6:10pm UTC
Last edited on Dec 7, 2019 at 6:28pm UTC
Dec 7, 2019 at 6:40pm UTC
Topic archived. No new replies allowed.