Click to See Complete Forum and Search --> : Template class organization
Zaei
Feb 14th, 2002, 03:14 PM
No, the problem comes when you actually compile the C++, instead of including it into a header. At that point, the code is already compiled into an obj, but when you try to instantiate, for instance A<int>, you get unresolved external symbols, because the actual implmentations of those functions never get compiled. I eventually changed the .cpp extensions to .inc, and included into the header, and everything works fine.
Z.
Technocrat
Feb 14th, 2002, 04:09 PM
This is SO FREAKY Zaei I was just going to ask that very same question!!!
So if any one knows how to do this I would like to know too.
All the examples I have found have all the template code in the header.
Technocrat
Feb 14th, 2002, 04:37 PM
I found the answer
Templates HAVE to be in the header, unless you have .cpp local templates
like:
template<class T> T Test(T &a, T &b)
{
T Temp = a;
a = b;
b = Temp;
return Temp;
}
void Test2()
{
int p, j, z;
p = 4;
j = 7;
z = Test<int>(p,j);
}
int WINAPI WinMain(HINSTANCE hInst, HINSTANCE hPrevInst, LPSTR lpCmdLine, int nCmdShow)
{
Test2();
....
Here is where I found the answer
http://www.codeguru.com/cgi-bin/bbs/wt/showpost.pl?Board=CPP&Number=2248&page=&view=&sb=
kedaman
Feb 14th, 2002, 04:43 PM
you are absolutely right, I never came to that situation (i generally always compile them altogether in one file)
The compiler will find it if you specify
A<int>::A()
{
}
but that's hardly usefull :/
might be an ugly solution but including a macro which with you can declare which instantces you need (and it will expand to specifications with the accurate contents)
but i'd say put them all in the same file.
kedaman
Feb 14th, 2002, 04:46 PM
well obviously the specific implementation can be in the cpp file, as long as the linker finds it.
Zaei
Feb 14th, 2002, 06:45 PM
Kind of a pain, but it makes sense.
Z.
kedaman
Feb 14th, 2002, 09:46 PM
And one thing, it can't inline can it?
Zaei
Feb 14th, 2002, 10:14 PM
I dont think templates can be inline, but Im not sure, as i dot use them very often =).
Z.
kedaman
Feb 14th, 2002, 10:24 PM
LOL, I would never even use templates if they wouldn't inline :D well, maybe, but that would suck insaneously!
templates advocates inlining!
Zaei
Feb 15th, 2002, 08:34 AM
Mah bad =). Like i said, I dont use them very much.
Z.
kedaman
Feb 15th, 2002, 05:01 PM
you should! you'll learn that most of the things you do, you only need to do once :)
parksie
Feb 16th, 2002, 01:58 PM
Originally posted by kedaman
you should! you'll learn that most of the things you do, you only need to do once :) Such as talking to Kedaman...you only need to say one thing and he'll provide you with endless bits of information :D
vbforums.com
Copyright Internet.com Inc., All Rights Reserved.