Solved

Compilation & linking of template classes

Posted on 1999-01-16
11
218 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

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

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Many modern programming languages support the concept of a property -- a class member that combines characteristics of both a data member and a method.  These are sometimes called "smart fields" because you can add logic that is applied automaticall…
Container Orchestration platforms empower organizations to scale their apps at an exceptional rate. This is the reason numerous innovation-driven companies are moving apps to an appropriated datacenter wide platform that empowers them to scale at a …
THe viewer will learn how to use NetBeans IDE 8.0 for Windows to perform CRUD operations on a MySql database.
The viewer will learn how to use the return statement in functions in C++. The video will also teach the user how to pass data to a function and have the function return data back for further processing.

695 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