Why the undefined references in this simple program?

H

here

I'm trying to complile and link this simple example of using a template:

//file stack2.hpp (header)
template<class T> class Stack {
T* v;
int max_size;
int top;
public:
class Underflow {};
class Overflow {};

Stack(int s); //constructor
~Stack(); //destructor

void push(T);
T pop();
};

//file stack2.cpp
#include "stack2.hpp"

template<class T> void Stack<T>::push(T c)
{
if (top==max_size) throw Overflow();
v[top] = c;
top = top + 1;
}
template<class T> T Stack<T>::pop()
{
if (top==0) throw Underflow();
top = top - 1;
return v[top];
}

//file user2.cpp
#include <iostream>
#include "stack2.hpp"
using namespace std;

Stack<char> sc(200);

int main(int argc, char *argv[])
{
sc.push('c');
cout << sc.pop() << "\n";

return 0;
}

/////////////////////////

If I compile with: g++ stack2.cpp user2.cpp
here's what I get:

/tmp/cciILMx2.o: In function `main':
user2.cpp:(.text+0x1a): undefined reference to `Stack<char>::push(char)'
user2.cpp:(.text+0x24): undefined reference to `Stack<char>::pop()'
/tmp/cciILMx2.o: In function `__static_initialization_and_destruction_0(int, int)':
user2.cpp:(.text+0x94): undefined reference to `Stack<char>::Stack(int)'
user2.cpp:(.text+0x99): undefined reference to `Stack<char>::~Stack()'
collect2: ld returned 1 exit status
 
Ö

Öö Tiib

I'm trying to complile and link this simple example of using a template:

//file stack2.hpp (header)

template<class T> class Stack {
T* v;
int max_size;
int top;
public:
class Underflow {};
class Overflow {};

Stack(int s); //constructor
~Stack(); //destructor

void push(T);
T pop();
};

//file stack2.cpp

Move its contents to stack2.h and delete the file.
This is how templates work.
 
H

here

Move its contents to stack2.h and delete the file.

This is how templates work.

I tried that and got the following:

$ g++ user.c
/tmp/cc9OwIxI.o: In function `f(Stack&, int)':
user.c:(.text+0x1f): undefined reference to `Stack::Stack(int)'
user.c:(.text+0x39): undefined reference to `Stack::Stack(int)'
user.c:(.text+0x4c): undefined reference to `Stack::push(char)'
user.c:(.text+0x5d): undefined reference to `Stack::push(char)'
user.c:(.text+0x6e): undefined reference to `Stack::push(char)'
user.c:(.text+0x7f): undefined reference to `Stack::push(char)'
user.c:(.text+0x89): undefined reference to `Stack::pop()'
user.c:(.text+0xb1): undefined reference to `Stack::pop()'
user.c:(.text+0xd9): undefined reference to `Stack::pop()'
user.c:(.text+0x101): undefined reference to `Stack::pop()'
user.c:(.text+0x129): undefined reference to `Stack::pop()'
user.c:(.text+0x151): undefined reference to `Stack::~Stack()'
user.c:(.text+0x179): undefined reference to `Stack::~Stack()'
/tmp/cc9OwIxI.o: In function `main':
user.c:(.text+0x1a5): undefined reference to `Stack::Stack(int)'
user.c:(.text+0x1b6): undefined reference to `Stack::push(char)'
user.c:(.text+0x1d3): undefined reference to `Stack::~Stack()'
user.c:(.text+0x1ee): undefined reference to `Stack::~Stack()'
/tmp/cc9OwIxI.o: In function `__static_initialization_and_destruction_0(int, int)':
user.c:(.text+0x246): undefined reference to `Stack::Stack(int)'
user.c:(.text+0x24b): undefined reference to `Stack::~Stack()'
collect2: ld returned 1 exit status
 
H

here

I tried that and got the following:



$ g++ user.c

/tmp/cc9OwIxI.o: In function `f(Stack&, int)':

user.c:(.text+0x1f): undefined reference to `Stack::Stack(int)'

user.c:(.text+0x39): undefined reference to `Stack::Stack(int)'

user.c:(.text+0x4c): undefined reference to `Stack::push(char)'

user.c:(.text+0x5d): undefined reference to `Stack::push(char)'

user.c:(.text+0x6e): undefined reference to `Stack::push(char)'

user.c:(.text+0x7f): undefined reference to `Stack::push(char)'

user.c:(.text+0x89): undefined reference to `Stack::pop()'

user.c:(.text+0xb1): undefined reference to `Stack::pop()'

user.c:(.text+0xd9): undefined reference to `Stack::pop()'

user.c:(.text+0x101): undefined reference to `Stack::pop()'

user.c:(.text+0x129): undefined reference to `Stack::pop()'

user.c:(.text+0x151): undefined reference to `Stack::~Stack()'

user.c:(.text+0x179): undefined reference to `Stack::~Stack()'

/tmp/cc9OwIxI.o: In function `main':

user.c:(.text+0x1a5): undefined reference to `Stack::Stack(int)'

user.c:(.text+0x1b6): undefined reference to `Stack::push(char)'

user.c:(.text+0x1d3): undefined reference to `Stack::~Stack()'

user.c:(.text+0x1ee): undefined reference to `Stack::~Stack()'

/tmp/cc9OwIxI.o: In function `__static_initialization_and_destruction_0(int, int)':

user.c:(.text+0x246): undefined reference to `Stack::Stack(int)'

user.c:(.text+0x24b): undefined reference to `Stack::~Stack()'

collect2: ld returned 1 exit status

SORRY THE ABOVE WAS THE WRONG FILE, HERE'S THE REAL OUTPUT:

$ g++ user2.cpp
/tmp/ccXm73vo.o: In function `__static_initialization_and_destruction_0(int, int)':
user2.cpp:(.text+0x94): undefined reference to `Stack<char>::Stack(int)'
user2.cpp:(.text+0x99): undefined reference to `Stack<char>::~Stack()'
collect2: ld returned 1 exit status
 
Ö

Öö Tiib

SORRY THE ABOVE WAS THE WRONG FILE, HERE'S THE REAL OUTPUT:

$ g++ user2.cpp
/tmp/ccXm73vo.o: In function `__static_initialization_and_destruction_0(int, int)':
user2.cpp:(.text+0x94): undefined reference to `Stack<char>::Stack(int)'
user2.cpp:(.text+0x99): undefined reference to `Stack<char>::~Stack()'
collect2: ld returned 1 exit status

The definitions for constructor and destructor *are* *missing*.
Define them in your stack.hpp.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,537
Members
45,021
Latest member
AkilahJaim

Latest Threads

Top