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

Templates with Microsoft compilers

Started by Geronimo2000 Jul 21, 2003 at 9:05 PM 5 replies 300+ views
Original Post
Geronimo2000
Geronimo2000
Hello - I am using Visual Studio 6.0 and .NET, and I am creating a bunch of different templatized classes. Some end up being interdependent (for example quaternion and matrix). With a non-templatized class I could just employ the old "forward declare in the .h file, #include in the .cpp file" procedure to avoid circular include compile errors. But, since I am forced to define all functions in the .h file, or a file included by the .h file for templatized classes, does anyone know of some nifty tricks I could use to emulate the previously described procedure for getting around circular includes? If I''ve totally overlooked something, please feel free to let me know as well. Thanks a lot for any help - If this problem can be solved without buying a new compiler, I''d love to know how. I have heard some talk about the "export" (?) keyword not being supported by Microsoft (and it being able to resolve this problem), but to tell the truth I''ve never used it, so I''m totally in the dark when it comes to that as well. G
sQuid
sQuid
do you mean this sort of thing?

#ifndef _MY_FUNKY_HDR
#define _MY_FUNKY_HDR
..
rest of header
..
#endif

That''s what I do anyway.
Geronimo2000
Geronimo2000
Thanks, but I don''t think that will work. I''ll still sthave to define all the functions in the header file, so I''ll still have the circular include problem...
Yohumbus
Yohumbus
Your going to have to forward declare the templated classes like

template
class To_Be_Referenced;

another thing you might do is seperate the implementation and declarations just like non-template classes but instead of one .h and one .cpp it is two .h''s. then you just have the implementation like a cpp file (I.E. no #ifndef) and include it just before the #endif at the end of the header.
ASCII stupid question, get a stupid ANSI
Geronimo2000
Geronimo2000
Thanks a lot - I'm unable to deduce what you mean by 2 header files, though. It sounds different than any strategy I've tried so I'm interested in it.
Maybe it would be easier if you or anyone else can diagram how they'd set their files up to accomodate the following two classes. This is one problem I've never solved, so I'm willing to believe there is no solution with Visual Studio...


template < typename T >
class Square
{
public:
Square(Triangle< T > *pTri);
Square() {};
};

template < typename T >
class Triangle
{
public:
Triangle(Square< T > *pSq);
Triangle() {}
};


To me the fundamental problem lies in needing to include all templatized class function definitions in the header file, .inl file included by the header file, etc. Therefore, I am never able to separate the interface from the implementation for one class to avoid the circular include. Am I wrong in thinking this is the essence of my problem, and/or wrong in thinking it makes the problem unsolvable???
Thanks so much for any help you can provide.

G



[edited by - Geronimo2000 on July 22, 2003 3:31:40 AM]

[edited by - Geronimo2000 on July 22, 2003 3:33:11 AM]

[edited by - Geronimo2000 on July 22, 2003 3:35:05 AM]
JD
JD
forward declare triangle at the very top like this:


template<class T> class Triangle;

template<typename T>
class Square
{
public:
Square(Triangle<T> *pTri){};
Square() {};
};

template<typename T>
class Triangle
{
public:
Triangle(Square<T> *pSq){};
Triangle() {}
};

Forged3D world editor

[edited by - JD on July 22, 2003 3:56:10 AM]

[edited by - JD on July 22, 2003 3:57:01 AM]
JD
JD
Looks like you will have to make inline func. def. if you''re using templatized classes. If you''re only using templated functions then you can separate function def. from class def. I just tried it on vc++6 with service pack 5. This will work:


Foo.h

class Foo
{
template<class T> void Bar(T i);
};

Foo.cpp

template<class T>
void Foo::Bar(T i){}

This won''t work:

template<class T>
class Foo
{
template<class C> void Bar(C i);
};

Foo.cpp
template<class C>
void Foo::Bar(C i){}

This will work:

template<class T>
class Foo
{
template<class C> void Bar(C i){};
};

Topic Locked

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

Sign in to reply to this topic.