943,861 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Unsolved
  • Views: 894
  • C++ RSS
Feb 20th, 2008
0

template class question

Expand Post »
First:
We know that it is better to write the declarations of a class in header file
And it's definition in another source file

BUT
if the class is template I have a problem to link the two files header and source it will appear two errors about linking


Second:
In the same class mentioned above I want to declare some friend functions has return type and input argument of the same class data type in the class and define it outside the class as usual

BUT
I have another problem in linking I don't know the reason


plz if any one have the answe support me

thank to all in advance
Similar Threads
Reputation Points: 7
Solved Threads: 2
Junior Poster in Training
farag is offline Offline
61 posts
since Feb 2008
Feb 20th, 2008
0

Re: template class question

Let's start with the first problem and see how that goes. Unless you're using a compiler that supports export (you probably aren't), templates can't be split into separate declaration and definition files. You have to provide the definition of the template in the header.
Administrator
Reputation Points: 6442
Solved Threads: 1393
Bad Cop
Narue is offline Offline
11,807 posts
since Sep 2004
Feb 20th, 2008
0

Re: template class question

Team Colleague
Reputation Points: 2780
Solved Threads: 312
long time no c
Dave Sinkula is offline Offline
4,790 posts
since Apr 2004
Feb 20th, 2008
0

Re: template class question

thank you Narue for your help

i want to told you i work on visual studio 2005

and what do you mean by export

thank you Dave Sinkula for your link
Reputation Points: 7
Solved Threads: 2
Junior Poster in Training
farag is offline Offline
61 posts
since Feb 2008
Feb 20th, 2008
0

Re: template class question

The reason for linker error is that the definition of the function template has not been instantiated. In order for a template to be instantiated, the compiler must know which definition should be instantiated and for what template arguments it should be instantiated. Unfortunately, these two pieces of information are in files that are compiled separately. Therefore, when our compiler sees the call to somefunction() but has no definition in sight to instantiate this function, it just assumes that such a definition is provided elsewhere and creates a reference (for the linker to resolve) to that definition. On the other hand, when the compiler processes the file(.cpp), it has no indication at that point that it must instantiate the template definition it contains for specific arguments.
SpS
Reputation Points: 70
Solved Threads: 32
Posting Pro
SpS is offline Offline
598 posts
since Aug 2005
Feb 21st, 2008
0

Re: template class question

>i want to told you i work on visual studio 2005
You don't have export.

>and what do you mean by export
It's a feature of C++ that supports what you're trying to do. But it's difficult to implement properly, and not many compilers have it.
Administrator
Reputation Points: 6442
Solved Threads: 1393
Bad Cop
Narue is offline Offline
11,807 posts
since Sep 2004
Feb 21st, 2008
0

Re: template class question

What about friend function problem
I work with the link passed by Dave Sinkula

I hope that I make it correctly
BUT the program has linking error

I have another question about

Reading Excel sheet columns and rows from C++ code
If there is any library support that pleas support me by it
Reputation Points: 7
Solved Threads: 2
Junior Poster in Training
farag is offline Offline
61 posts
since Feb 2008

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C++ Forum Timeline: processing a char of serial data
Next Thread in C++ Forum Timeline: Wow I'm really stuck on this one. I need to make a rectangle out of symbols.





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC