Jump to content
  • Advertisement

Archived

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

scrawny

template trouble

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

If you intended to correct an error in the post then please contact us.

Recommended Posts

I've never used templates before, so I decided to test them out with this VERY simple console program. Here's the code: Container.h: #ifndef _CONTAINER_H_ #define _CONTAINER_H_ template <class T> class Container { public: Container(T *); ~Container(); T & GetObject() const; protected: T * pObject; }; #endif //_CONTAINER_H_ Container.cpp: #include "Container.h" template <class T> Container<T>::Container(T * pInitObject): pObject(pInitObject) { } template <class T> Container<T>::~Container() { delete pObject; } template <class T> T & Container<T>::GetObject() const { return *pObject; } TemplateTest.cpp: #include "container.h" #include <iostream.h> int main() { int i = 10; Container<int> container(&i); cout << container.GetObject(); return 0; } That's it. Just a simple little test. My linker tells me all the member functions for Container<int> are unresolved externals. I thought the compiler was supposed to define that for you. That's the point of a template, right? I double and triple checked that Container.cpp was included in my project and that the member fuctions' declarations matched the definitions. I know it must be something obvious. Edited by - scrawny on 4/29/00 1:14:35 AM

Share this post


Link to post
Share on other sites
Advertisement
Although it''s supposed to work the way you have done it, most (all?) of the current major compiler versions don''t do it this way. The implementations must be in the same file as the declarations, or at least must be able to be accessed. If you want to put them in a cpp file, then you''re gonna have to

#include "container.cpp"

at the bottom of container.h. That''s ugly, so I just type the functions at the bottom of the h file, or make them inline (they wont be inlined if they''re too big anyway).

The reason for this is that the compiler needs to be able to see the class and functions when it''s creating versions of the class for all the types you use it with. That''s all it does really (as far as I know), is recompile the class for all the types you use it with.

Hope this helps.

By the way, Borland C++Builder 5 *might* support templates the way you are doing them. The only reason I say this is because Borland seems to pick up the standard first among the major compiler vendors. But I''m not sure.

Hope I helped!

------------------------------
Jonathan Little
invader@hushmail.com
http://www.crosswinds.net/~uselessknowledge

Share this post


Link to post
Share on other sites
Yeah, Borland is fairly up to date with some features, but I don''t think the one you''re talking about (the export keyword) is supported by ANY compiler. Further, I''m not sure ANY vendor even has plans to support it in the near future. Ugh.

-Brian

Share this post


Link to post
Share on other sites
Thanks. It compiles now, but I''m still having a problem. When I compile it in release mode, it''s fine. When I compile it in debug mode, it tells me a debug assertation failed in my program in dbgdel.cpp at line 47. The expression is: _BLOCK_TYPE_IS_VALID(pHead->nBlockUSE). It''s happening when main() is returning.

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
you''re trying to delete a variable that was allocated on the stack.

Share this post


Link to post
Share on other sites
OOPS! Stupid mistake! I figured it out. Thanks everybody.

Edited by - scrawny on 4/29/00 12:51:16 PM

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
yes.

when you new something, where does the memory come from? the heap. when you delete something, where does it go? back to the heap.

when you declare "int i" in a function, how does the memory get allocated? by incrementing the stack. when "int i" goes out of scope, at the end of the function, how does the memory det "deallocated"? by decrementing the stack.

so, you''re taking a pointer into your Container template of a variable (int i) that is on the stack. At the end of main, when the Container goes out of scope, and the destructor gets called, it tries to delete an address that is in the stack, not the heap.

you should only be deleting something Container''s destructor if 1) you know it came from the heap, and 2) know that no one else is going to use that pointer (which also includes trying to also delete the pointer).

Share this post


Link to post
Share on other sites
I case anybody was wondering, my message that says "OOPS! Stupid mistake! ..." originally said "Could it be in my deconstructor? Where I delete pObject?" Then I saw my mistake.

Share this post


Link to post
Share on other sites

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!