• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 223
  • Last Modified:

Compilation & linking of template classes

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
smith_c
Asked:
smith_c
  • 3
  • 2
  • 2
  • +4
1 Solution
 
ahTriCommented:
U better include the source code and error message
0
 
Answers2000Commented:
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
 
smith_cAuthor Commented:
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
Get expert help—faster!

Need expert help—fast? Use the Help Bell for personalized assistance getting answers to your important questions.

 
resinCommented:
Answers2000's right! You cannot do it in another way. It is just the way template works.
0
 
jkrCommented:
resin - there's nothing to add ;-)
Answers2000's answer is definitely OK - no need to reject it...
0
 
DOMINIC011899Commented:
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
 
Tommy HuiEngineerCommented:
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
 
Tommy HuiEngineerCommented:
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
 
smith_cAuthor Commented:
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
 
smith_cAuthor Commented:
Will 'Answers2000' please submit their answer again, so that I can allocate the points please.
0
 
Answers2000Commented:
Thanks

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

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Get expert help—faster!

Need expert help—fast? Use the Help Bell for personalized assistance getting answers to your important questions.

  • 3
  • 2
  • 2
  • +4
Tackle projects and never again get stuck behind a technical roadblock.
Join Now