template class question

Please support our C++ advertiser: Intel Parallel Studio Home
Reply

Join Date: Feb 2008
Posts: 51
Reputation: farag is an unknown quantity at this point 
Solved Threads: 1
farag farag is offline Offline
Junior Poster in Training

template class question

 
0
  #1
Feb 20th, 2008
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
Reply With Quote Quick reply to this message  
Join Date: Sep 2004
Posts: 7,850
Reputation: Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute 
Solved Threads: 754
Team Colleague
Narue's Avatar
Narue Narue is offline Offline
Senior Bitch

Re: template class question

 
0
  #2
Feb 20th, 2008
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.
New members chased away this month: 4
Reply With Quote Quick reply to this message  
Join Date: Apr 2004
Posts: 4,461
Reputation: Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future 
Solved Threads: 254
Team Colleague
Dave Sinkula's Avatar
Dave Sinkula Dave Sinkula is offline Offline
long time no c

Re: template class question

 
0
  #3
Feb 20th, 2008
"One of the methods used by statists to destroy capitalism consists in establishing controls that tie a given industry hand and foot, making it unable to solve its problems, then declaring that freedom has failed and stronger controls are necessary." --Ayn Rand
Reply With Quote Quick reply to this message  
Join Date: Feb 2008
Posts: 51
Reputation: farag is an unknown quantity at this point 
Solved Threads: 1
farag farag is offline Offline
Junior Poster in Training

Re: template class question

 
0
  #4
Feb 20th, 2008
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
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 598
Reputation: SpS is on a distinguished road 
Solved Threads: 32
SpS's Avatar
SpS SpS is offline Offline
Posting Pro

Re: template class question

 
0
  #5
Feb 20th, 2008
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.
Reply With Quote Quick reply to this message  
Join Date: Sep 2004
Posts: 7,850
Reputation: Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute 
Solved Threads: 754
Team Colleague
Narue's Avatar
Narue Narue is offline Offline
Senior Bitch

Re: template class question

 
0
  #6
Feb 21st, 2008
>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.
New members chased away this month: 4
Reply With Quote Quick reply to this message  
Join Date: Feb 2008
Posts: 51
Reputation: farag is an unknown quantity at this point 
Solved Threads: 1
farag farag is offline Offline
Junior Poster in Training

Re: template class question

 
0
  #7
Feb 21st, 2008
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
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:




Views: 775 | Replies: 6
Thread Tools Search this Thread



Tag cloud for C++
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC