| | |
template class question
Please support our C++ advertiser: Intel Parallel Studio Home
![]() |
•
•
Join Date: Feb 2008
Posts: 50
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.
I'm here to prove you wrong.
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.
| Thread Tools | Search this Thread |
api array arrays based beginner binary bitmap c++ c/c++ calculator char class classes code compile compiler console conversion count delete deploy desktop directshow dll download dynamic dynamiccharacterarray encryption error file forms fstream function functions game getline givemetehcodez google graph gui homeworkhelp homeworkhelper iamthwee ifstream input int integer java lib linkedlist linker linux list loop looping loops map math matrix memory news node output parameter pointer problem program programming project proxy python read recursion recursive reference return rpg string strings struct temperature template templates test text text-file tree unix url variable vector video visual visualstudio win32 windows winsock word wordfrequency wxwidgets






