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>:ush(AbstractType val)
{
// Increment index, then store
data[++index] = val;
}
template <class AbstractType>
AbstractType Stack<AbstractType>:op()
{
// 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)"
---------------------------------------------------------------------------
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>:ush(AbstractType val)
{
// Increment index, then store
data[++index] = val;
}
template <class AbstractType>
AbstractType Stack<AbstractType>:op()
{
// 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)"
---------------------------------------------------------------------------