Solved

Compilation & linking of template classes

Posted on 1999-01-16
11
214 Views
Last Modified: 2013-12-14
Please help me, I am new to Microsoft Visual C++, and I am
experiencing a problem with template classes.

I have defined and created a template class (tStack) in a source code file (stack.h and stack.cpp).  I now want to use the stack class (of type tStack<long>) in my program.  All of the source code files compile without errors, but during the link phase the linker reports that it can not link the appropriate class member functions.

I assume that the compiler is not instantiating the class tStack<long>, and this is what is causing the problem - but I do not know how to solve the problem.  I would be grateful of a solution.

Thankyou in anticipation.
0
Comment
Question by:smith_c
  • 3
  • 2
  • 2
  • +4
11 Comments
 

Expert Comment

by:ahTri
ID: 1182323
U better include the source code and error message
0
 
LVL 8

Expert Comment

by:Answers2000
ID: 1182324
This is a common problem that comes up in EE

You need to stick the tStack member functions in the .h file instead of the .cpp

Either place them  in the body of the class definition in the .h, or use inline

//Like this

template <class T> class Example 1
{
public:
    void SomeFunc(T param)   { /*code_goes_here*/ }
} ;

//or

template <class T> class Example2
{
public:
   void SomeFunc( T param ) ;
} ;

inline void Example2<T>::SomeFunc( T param )
{
// code goes here
}

The compiler can not expand the template otherwise (except internal within stack.cpp)

To instantiate the template from another source file, do (make sure stack.h is #included before this)

Example1<char> example_variable1 ;
Example2<char> example_variable2 ;

replacing char with your appropriate variable type
0
 

Author Comment

by:smith_c
ID: 1182325
I have rejected 'Answers2000's answer pending further information, from 'ahTri'.  The compiler I am used to (Silicon Graphics C++ v4.01) does not require both declaration & implementation of member functions in the header files, and I am wondering if it is possible to do this with MS VC++ v6.  If there is no other way than that proposed by 'Answers2000', then rest assured, I will cough up the points (to 'Answers2000' as they where the first to respond to my SOS), and wend my merry way.  More information follows...



Here is some more information, relating to the replies from
'Answers2000' and 'ahTri'

This is the header file "stack.h"

template <class tNodeData>
class tStack
{
 public:
 
 tStack () ;
 tStack (const tStack& CopyStack) ;
 ~tStack () ;

 void Push (const tNodeData& NewItem) ;
 tNodeData& Pop () ;
 unsigned long Size () ;
 bool IsEmpty () ;
 .
 .
 .
} ;




This is the source file "stack.cpp"

#include "stack.h"

template <class tNodeData>
tStack<tNodeData>::tStack ()
{
 sStackBottom = NULL ;
 sStackTop = NULL ;
 lStackSize = 0 ;
}

template <class tNodeData>
tStack<tNodeData>::tStack (const tStack& CopyStack)
{
 ReplicateStack (CopyStack) ;
}

 .
 .
 .



This is the file used to test the class 'tStack'

#include <iostream.h>
#include "stack.h"

int
main ()
{
 long            iIter ;
 tStack<long>   LongStack ;  
 
 for (iIter = 0 ; iIter < 2000 ; iIter++)
  LongStack.Push (iIter) ;


 while (!LongStack.IsEmpty ())
  cout << LongStack.Pop () << endl ;  
 
 
 return (0) ;
}


The reported compiler error is...

 main.obj: error LNK2001 unresolved external symbol
"public long &__thiscall tStack<long>::Pop(void)" ....


0
 

Expert Comment

by:resin
ID: 1182326
Answers2000's right! You cannot do it in another way. It is just the way template works.
0
 
LVL 86

Expert Comment

by:jkr
ID: 1182327
resin - there's nothing to add ;-)
Answers2000's answer is definitely OK - no need to reject it...
0
What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

 

Expert Comment

by:DOMINIC011899
ID: 1182328
If your purpose is using a stack regardless who made it, I recommend the STL stack.  (But if you're studying templates,
disregard this).


#include <STACK>
using namespace std;



// Declare a 'long' stack
stack<long> stacklong;



//  Put something in the stack
for ( long lNum=0L;  lNum<10;  lNum++ )
    stacklong.push( lNum );  // Push on top of stack

//  Pop them out
while ( !stacklong.empty() )
    {
    // Get a reference (more efficient)
    long &rlNum = stacklong.top();  
    // Display it out
    cout << rlNum << " ";
    // Remove it from top of stack
    stacklong.pop();
    }

0
 
LVL 15

Expert Comment

by:Tommy Hui
ID: 1182329
Another way to do it, if you don't want to modify your .h and .cpp files is to add at the bottom of your stack.h file

#include "stack.cpp"


0
 
LVL 15

Expert Comment

by:Tommy Hui
ID: 1182330
Hmmm., my comment went blank.

Another way to do this is to add

#include "stack.cpp"

at the bottom of your stack.h file. This does not require any other changes to your files. All you have to do is recompile.
0
 

Author Comment

by:smith_c
ID: 1182331
I apologise for the fact I ever doubted you 'Answers2000'.

Now, how can I accept your original proposal, and give you the 50 points (plus 10 by ways of an apology).
0
 

Author Comment

by:smith_c
ID: 1182332
Will 'Answers2000' please submit their answer again, so that I can allocate the points please.
0
 
LVL 8

Accepted Solution

by:
Answers2000 earned 60 total points
ID: 1182333
Thanks

BTW you get your dummyQ's points back - see http://www.experts-exchange.com/jsp/qShow.jsp?ta=commspt&qid=10124434
0

Featured Post

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Article by: SunnyDark
This article's goal is to present you with an easy to use XML wrapper for C++ and also present some interesting techniques that you might use with MS C++. The reason I built this class is to ease the pain of using XML files with C++, since there is…
C++ Properties One feature missing from standard C++ that you will find in many other Object Oriented Programming languages is something called a Property (http://www.experts-exchange.com/Programming/Languages/CPP/A_3912-Object-Properties-in-C.ht…
This tutorial covers a step-by-step guide to install VisualVM launcher in eclipse.
The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.

743 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now