473,396 Members | 1,917 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

Need help with a Linker problem with class template.

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)"
---------------------------------------------------------------------------
Jul 22 '05 #1
1 1722
Mario Rosario wrote:
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-lit...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)"
---------------------------------------------------------------------------

--
Artie Gold -- Austin, Texas
http://it-matters.blogspot.com (new post 12/5)
http://www.cafepress.com/goldsays
Jul 22 '05 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: Victor Hannak | last post by:
This is my first try at using a template class, and I am getting linker errors that I can't figure out: Unresolved external 'ShapeStackClass<CircleClass>::GetStackSize()' referenced from...
6
by: Rex_chaos | last post by:
Hi all, I am writing a project in C++. I am going to compile all my source to a library(.a). So I can only distribute the lib as well as the header file to my client. However, I don't quite...
1
by: David2511 | last post by:
Hello, I need a little help. I try to write the following architecture : an abstract template class A Two classes derived from class A : the classes B and C which are concrete. The class...
5
by: tuko | last post by:
The following snipet gives a linker error. I don't get it... template<class T> class tran { public: public: private: }; template<class T> class matrix {
11
by: Micha | last post by:
Hello there, I think I've run into some classic c++ pitfall and maybe some of you guys can help me out. For my project I will need to use matrices and vectors and so I decided to implement them...
1
by: giganut | last post by:
Hi, I'm using MS VS.Net2003, and I'm getting a linker error in a project for a templated friend function Specifically: template <class Tclass Vec3 {...
5
by: 2beagles | last post by:
I have a template for a three dimensional array that I have been working on using Visual C++ 6.0. Under version 6 this code worked fine. However, last night I downloaded Visual C++ 2005 Express and...
7
by: meLlamanJefe | last post by:
The following is an abridged copy of the code I am using in a template function along with the linker error produced during the build. The compiler has no issues with the code, only the linker: ...
1
bajajv
by: bajajv | last post by:
Hi, While implementing CList class, I got linker errors for constructor and destructor. Below is the code - template<class TYPE, class ARG_TYPE = const TYPE&> class CList { protected: struct...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.