Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

Compilation & linking of template classes

Posted on 1999-01-16
11
Medium Priority
?
220 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
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 

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 240 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

Tech or Treat! - Giveaway

Submit an article about your scariest tech experience—and the solution—and you’ll be automatically entered to win one of 4 fantastic tech gadgets.

Question has a verified solution.

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

IntroductionThis article is the second in a three part article series on the Visual Studio 2008 Debugger.  It provides tips in setting and using breakpoints. If not familiar with this debugger, you can find a basic introduction in the EE article loc…
Jaspersoft Studio is a plugin for Eclipse that lets you create reports from a datasource.  In this article, we'll go over creating a report from a default template and setting up a datasource that connects to your database.
The viewer will be introduced to the technique of using vectors in C++. The video will cover how to define a vector, store values in the vector and retrieve data from the values stored in the vector.
The viewer will be introduced to the member functions push_back and pop_back of the vector class. The video will teach the difference between the two as well as how to use each one along with its functionality.
Suggested Courses

618 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