| | |
template class question
Please support our C++ advertiser: Intel Parallel Studio Home
![]() |
•
•
Join Date: Feb 2008
Posts: 51
Reputation:
Solved Threads: 1
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
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
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
http://www.parashift.com/c++-faq-lit...html#faq-35.13 or the next several.
"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
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.
![]() |
Similar Threads
- help (overload << in a template class ) (C++)
- Difference between a Template Class and a Class Template (C++)
- template declaration (C++)
- overloaded operators question (C)
- Question (C++)
- another question (C++)
- Is it possible to forward declare a template? (C)
- Template in *.h or *.cpp (C)
- class toString() functionality in C++ ...? (C++)
Other Threads in the C++ Forum
- Previous Thread: processing a char of serial data
- Next Thread: Wow I'm really stuck on this one. I need to make a rectangle out of symbols.
Views: 775 | Replies: 6
| Thread Tools | Search this Thread |
Tag cloud for C++
6 api array arrays based beginner binary bmp c++ c/c++ calculator char class classes code compile compiler console conversion convert count data delete deploy dll download dynamic dynamiccharacterarray encryption error file format forms fstream function functions game givemetehcodez graph gui homeworkhelp iamthwee ifstream input int java lib library lines linker list loop looping loops map math matrix memory newbie news number output pointer problem program programming project python random read recursion recursive reference return rpg search simple sort spoonfeeding string strings struct temperature template templates text text-file tree url variable vector video visual visualstudio void win32 windows winsock wordfrequency wxwidgets






