Need help with a Linker problem with class template.

M

Mario Rosario

Hi,

Thanks in advance for your help.

I have a compile problem; actually a link problem, when I compile the class
template below. I would appreciate if anyone can tell me what is wrong and
how I can fix this.

I coded a stack in one file Stack.cpp, it compiles fine and works fine but
when I seperated this code into three files: Stack.h, Stack.cpp and
Main.cpp, I get a link problem. The compiler says it doesn't know what Stack
is. This happens in both Linux and Visual C++. I include Stack.h, Stack.cpp,
Main.cpp, the Linux Makefile and the errors below.

What gives?

Thanks again,
Mario

Stack.h ------------------------------------------
#include <iostream>
using namespace std;

template <class AbstractType>
class Stack {
public:
Stack();
~Stack();
void push(AbstractType val);
AbstractType pop();

private:
AbstractType data[100];
int index;
}

Stack.cpp ----------------------------------------
#include "Stack.h"

template <class AbstractType>
Stack<AbstractType>::Stack() : index(-1) {}

template <class AbstractType>
Stack<AbstractType>::~Stack() {}

template <class AbstractType>
void Stack<AbstractType>::push(AbstractType val)
{
// Increment index, then store
data[++index] = val;
}

template <class AbstractType>
AbstractType Stack<AbstractType>::pop()
{
// Retrieve, then decrement index
return data[index--];
}

Main.cpp ------------------------------------------------
#include "Stack.h"

int main()
{
// Template instantiation
Stack<int> stack;
int num;
int den;

cout << "Enter num: ";
cin >> num;
cout << "Enter den: ";
cin >> den;

stack.push(num);
stack.push(den);

int val = stack.pop();
cout << "popped " << val << endl;
val = stack.pop();
cout << "popped " << val << endl;

return 0;
}

Makefile -------------------------------------------------
C++ = c++
CCFLAGS = -g

all: main

Stack.o: Stack.cpp
${C++} ${CCFLAGS} -c Stack.cpp

Main.o: Main.cpp
${C++} ${CCFLAGS} -c Main.cpp

main: Main.o Stack.o
${C++} ${CCFLAGS} Main.o Stack.o -o main

clean:
rm *.o main

Linux error--------------------------------------------------------------
c++ -g Main.o Stack.o -o main
Main.o: In function `main':
Main.cpp:9: undefined reference to `Stack<int>::Stack[in-charge]()'

Visual C++ error----------------------------------------------------------
Linking...
Project2.obj : error LNK2019: unresolved external symbol "public: __thiscall
Stack<int>::~Stack<int>(void)"
---------------------------------------------------------------------------
 
A

Artie Gold

Mario said:
Hi,

Thanks in advance for your help.

I have a compile problem; actually a link problem, when I compile the class
template below. I would appreciate if anyone can tell me what is wrong and
how I can fix this.

See, for example:

http://www.parashift.com/c++-faq-lite/containers-and-templates.html#faq-34.12
(Of course, reading the *entire* FAQ would be a *really* good idea as well.)

HTH,
--ag
I coded a stack in one file Stack.cpp, it compiles fine and works fine but
when I seperated this code into three files: Stack.h, Stack.cpp and
Main.cpp, I get a link problem. The compiler says it doesn't know what Stack
is. This happens in both Linux and Visual C++. I include Stack.h, Stack.cpp,
Main.cpp, the Linux Makefile and the errors below.

What gives?

Thanks again,
Mario

Stack.h ------------------------------------------
#include <iostream>
using namespace std;

template <class AbstractType>
class Stack {
public:
Stack();
~Stack();
void push(AbstractType val);
AbstractType pop();

private:
AbstractType data[100];
int index;
}

Stack.cpp ----------------------------------------
#include "Stack.h"

template <class AbstractType>
Stack<AbstractType>::Stack() : index(-1) {}

template <class AbstractType>
Stack<AbstractType>::~Stack() {}

template <class AbstractType>
void Stack<AbstractType>::push(AbstractType val)
{
// Increment index, then store
data[++index] = val;
}

template <class AbstractType>
AbstractType Stack<AbstractType>::pop()
{
// Retrieve, then decrement index
return data[index--];
}

Main.cpp ------------------------------------------------
#include "Stack.h"

int main()
{
// Template instantiation
Stack<int> stack;
int num;
int den;

cout << "Enter num: ";
cin >> num;
cout << "Enter den: ";
cin >> den;

stack.push(num);
stack.push(den);

int val = stack.pop();
cout << "popped " << val << endl;
val = stack.pop();
cout << "popped " << val << endl;

return 0;
}

Makefile -------------------------------------------------
C++ = c++
CCFLAGS = -g

all: main

Stack.o: Stack.cpp
${C++} ${CCFLAGS} -c Stack.cpp

Main.o: Main.cpp
${C++} ${CCFLAGS} -c Main.cpp

main: Main.o Stack.o
${C++} ${CCFLAGS} Main.o Stack.o -o main

clean:
rm *.o main

Linux error--------------------------------------------------------------
c++ -g Main.o Stack.o -o main
Main.o: In function `main':
Main.cpp:9: undefined reference to `Stack<int>::Stack[in-charge]()'

Visual C++ error----------------------------------------------------------
Linking...
Project2.obj : error LNK2019: unresolved external symbol "public: __thiscall
Stack<int>::~Stack<int>(void)"
 

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

Forum statistics

Threads
473,776
Messages
2,569,602
Members
45,185
Latest member
GluceaReviews

Latest Threads

Top