undefined reference to - error
Sep 23, 2010 at 12:17pm UTC
My code seems to give a number of "undefined reference to" errors.
Here's the code:
main.cc
1 2 3 4 5 6 7 8 9 10 11 12 13 14
#include <iostream>
#include "stack1.h"
using namespace std;
int main(){
int k;
stack<int > x;
x.push(1);
x.push(7);
x.pop(k);
cout << k;
return 0;
}
stack.h
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
#ifndef __stack1_h__
#define __stack1_h__
using namespace std;
const int MAX = 100;
template <class T>
class stack{
public :
stack();
~stack();
bool pop(T&);
bool push(T);
bool full();
bool empty();
private :
T stackitems[MAX];
int stacksize;
};
#endif
stack.cc
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
#include "stack1.h"
using namespace std;
template <class T>
stack<T>::stack(){
stacksize = -1;
}
template <class T>
bool stack<T>::pop(T& item){
if (!empty()){
item = stackitems[stacksize];
stacksize--;
return true ;
}
return false ;
}
template <class T>
bool stack<T>::push(T item){
if (!full()){
stacksize++;
stackitems[stacksize] = item;
return true ;
}
return false ;
}
template <class T>
bool stack<T>::full(){
if (stacksize+1==MAX){
return true ;
}
return false ;
}
template <class T>
bool stack<T>::empty(){
if (stacksize < 0){
return true ;
}
return false ;
}
the errors I get are:
1 2 3 4 5 6
<snip>/main.cc:8: undefined reference to `stack<int >::stack()'
<snip>/main.cc:9: undefined reference to `stack<int>::push(int)'
<snip>/main.cc:10: undefined reference to `stack<int >::push(int )'
<snip>/main.cc:11: undefined reference to `stack<int>::pop(int&)'
<snip>/main.cc:13: undefined reference to `stack<int >::~stack()'
<snip>/main.cc:13: undefined reference to `stack<int>::~stack()'
compiled with g++, makefile made using qmake.
Can anyone tell me why I'm getting these errors?
Sep 23, 2010 at 12:46pm UTC
Last edited on Sep 23, 2010 at 12:48pm UTC
Sep 23, 2010 at 12:53pm UTC
That worked, thanks.
I used at least 3 different tutorials for template classes, you'd think one of them would mention it...
Topic archived. No new replies allowed.