#### Archived

This topic is now archived and is closed to further replies.

# Inheriting from templated classes

This topic is 6717 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

Hi, I''m having some trouble with my templated base class. I can compile the code, but when I try to link, i get unresolved externals for every member function in the base class. I have the member functions defined outside of the class declaration, but when I define the functions inside the class, there''s no linking errors. I''m using VC++ 6.0. Even when I double click on the member function in the class view, I get an error saying it can''t find the definitions either. I''ve made sure the .cpp file is part of the project as well. I''m really stuck as i don''t want to define the functions in the class, as they''re kinda large. Any help would be appreciated. Thanks. ekaj

##### Share on other sites
Not to be to obvious, but did you remember to put "::" in front of the member function names when you defined them in your .cpp file?

##### Share on other sites
... stupid dumb self ... that''s "classname::" ... (I had angle brackets around classname and the system ate it - I knew it would do that, too.)

##### Share on other sites
Yeah, i remembered to do that. The .cpp file compiles no prob.. it''s when it''s linking that i get the problem. For now I guess I''ll just define them all in the class... it''s a pain.. but will let me continue.

Does that necessarily mean the function will be inlined?

ekaj

##### Share on other sites
Well I can''t explain why, exactly, but templated classes have to have all of their functions inlined when you compile with MSVC6. I think it''s a limitation of C++, but it might just be the compiler.

The only way to work around this, is to include in the bottom of you cpp file:
extern template class;

and others of that form, where int is what you want it to template. Generally it''s easier to put it all inline rather than having to know all of the externs during compilation. (since using the template in a new place will require a change to the template class'' file as well.

Lord Pi

Dark Lord Pi

##### Share on other sites
You should put the template definitions and declarations in the header file, otherwise VC++ can''t instantiate the templates.
If you have all the member definitions in a .cpp file, you can include that .cpp file at the bottom of the .h file. Be sure to remove the .cpp file from the project!

Lord Pi: unfortunately, the keyword ''extern'' isn''t supported by many compilers, including VC++ 6. The keyword is recognised syntactically, but not semantically. Allmost every C++ compiler uses the inclusion compile method for templates, which means that declarations and definitions should be in the same file. The other way is the separation compile method, in which definitions and declarations can be in a different header and source file.

Erik

##### Share on other sites
Thanks for your help. I''ve just defined them all in the class declaration and it''s working. Just seems a little dumb to have to do that. Thanks again..

ekaj

##### Share on other sites
The template-implementation-in-header-file thing makes sense, even though it is frustrating. In C++, each module is compiled as a stand-alone unit: this is why you need function prototypes, so that it is possible to call a function without seeing the definition, as you only need to know what the name of the function will be and how to pass the parameters. Now, to make use of a template in one of these modules, the compiler has to generate code for the class so that your code can call it. If your file uses MyTemplateClass<class T> parameterised by int, it needs to create a MyTemplateClass<int> for this particular module, so that you can call the methods, etc. Therefore, the function definitions need to be available at the compilation stage so that your code can use them, as there is no guarantee it has already been instantiated in any other module.

The current lack of support for the ''export'' keyword (I believe it''s export and not extern, as people have been saying here, but I could be wrong) is because that although it would make more sense from a C++ language point of view to hide the implementation of a template, it makes it a lot more complex for the compiler developers, for the reasons I stated above.

##### Share on other sites
Heheh
I wish you could attach files to these post...

Here's a template LinkList Constructor defined out of the template class definition:

  template class LinkedList{ public: //Constructors LinkedList(); LinkedList(const LinkedList&); ~LinkedList(); //on and on...};template LinkedList::LinkedList() { //These are private members of the template class Limit_of_Container = -1; Items_in_Container = 0; First = Last = Current = 0; //I suppose NULL would make better sense, but that's C style coding... Probes =0; }

Edited by - Magmai Kai Holmlor on June 25, 2000 4:16:01 PM

##### Share on other sites
Erik Post, Kylotan:
Look up ''Explicit Instantiation'' in MSDN. It lists extern as a VC++6 specifc keyword. So it is ''extern'' and it is recognized by VC++6. I''m using it in some code I''ve currently using as well.

Although... you are both right in regards to export. Export is the keyword defined by the standard, and no one currently recognizes it.

Anyways... I believe that ekaj''s question was answered enough times, so I''m sorry if this adds confusion or starts a flame war.

Dark Lord Pi

1. 1
Rutin
34
2. 2
3. 3
4. 4
5. 5

• 12
• 14
• 9
• 9
• 9
• ### Forum Statistics

• Total Topics
633331
• Total Posts
3011399
• ### Who's Online (See full list)

There are no registered users currently online

×