Solved

Compilation & linking of template classes

Posted on 1999-01-16
11
216 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
Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

 

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
 

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

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
eclipse luna javeEE perspecive missing 5 114
Eclipse Neon start with Admin account only 6 144
operator overload: incompatible type and too many params 5 84
oracle 11g 23 107
  Included as part of the C++ Standard Template Library (STL) is a collection of generic containers. Each of these containers serves a different purpose and has different pros and cons. It is often difficult to decide which container to use and …
Update (December 2011): Since this article was published, the things have changed for good for Android native developers. The Sequoyah Project (http://www.eclipse.org/sequoyah/) automates most of the tasks discussed in this article. You can even fin…
This tutorial covers a step-by-step guide to install VisualVM launcher in eclipse.
The goal of the video will be to teach the user the difference and consequence of passing data by value vs passing data by reference in C++. An example of passing data by value as well as an example of passing data by reference will be be given. Bot…

821 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