Skip to main content
GameDev.net gamedev.net
🔒 Locked

template trouble

Started by scrawny Apr 29, 2000 at 1:09 AM 10 replies 800+ views
Original Post
scrawny
scrawny
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
Qoy
Qoy
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
osmanb
osmanb
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
scrawny
scrawny
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.
scrawny
scrawny
Also, if I click retry, it gives me a GPF.
scrawny
scrawny
OOPS! Stupid mistake! I figured it out. Thanks everybody.

Edited by - scrawny on 4/29/00 12:51:16 PM
scrawny
scrawny
Yeah. I realized that the second I posted that and edited the message.
scrawny
scrawny
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.
CtrlAltDelete
CtrlAltDelete
hmmmm I found this code interesting and challenging since I''m new to templates, however I couldn’t fix the errors based on the comments that were given. Could someone help me out by posting a fixed version of the code from the original post?

Thanks, Chuck
scrawny
scrawny
Sure CtrlAltDelete.

Container.h:

#ifndef _CONTAINER_H_
#define _CONTAINER_H_

template <class T> class Container
{
public:
Container(T *);
~Container();
T & GetObject() const;
private:
T * pObject;
};

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;
}

#endif //_CONTAINER_H_

TemplateTest.cpp:

#include "container.h"
#include <iostream.h>

int main()
{
int * pInt = new int;
*pInt = 10;
Container<int> container(pInt);
cout << container.GetObject();
return 0;
}

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.