VC++ 6 Class Template Linking Issue

Started by
13 comments, last by coderx75 20 years, 1 month ago
I''ve exhausted all of my resources on fixing this issue except for my last resort, which I only use in these rare cases, the gamedev.net fora. Here''s my prob: I began working with Class Templates this weekend (yeah, I''m a little behind the times) and decided to work with the simplest and most common of templates: a simple array class which performs bounds checking. I started off with a regular class which acts as a char array, overloaded the [] operator to access it, instantiated the object and tested it. This is with the class def in its own header file and the implementation in a .cpp file. Works fine. Yay. Now, I converted the class to a class template. Everything compiles fine but the linker doesn''t see the array.obj file. So, I took the class template definition from the header file, the code from the .cpp file and placed it directly in with the .cpp file that instantiates the array. Now everything compiles AND links. I run it and it works beautifuly, however, it''d be a cold day in hell that I''d ever structure my code like this. Obviously I''ve implemented this correctly but the linker doesn''t seem to want to accept it. Yes, my array.cpp file is including the array.h file. Yes, my program is also including the array.h file. And yes, both array.cpp and array.h have been added to my project file. Has anybody else had this issue with class templates? Anyone manage to resolve this? Any help is greatly appreciated. Thanks, pplz! - Jay
Quit screwin' around! - Brock Samson
Advertisement
I hate to say it, but you''re going to have to live with this problem. Due to complicated technical issues, it is incredibly difficult to write compilers to work with templates that have separated declarations/definitions. I believe there has only been one compiler that supports it, and they wasted tons of time implementing it, and no one else is going to bother putting it into their compiler. I ran into this problem as well way back when, and I was totally lost. I had perfect syntax, but no, it just wouldn''t link. Then I read about the issue in a game programming book at a bookstore that same week. What a coincident. And I was miffed as well that I was gonna have to live with it. Oh well. I got use to it.
"We should have a great fewer disputes in the world if words were taken for what they are, the signs of our ideas only, and not for things themselves." - John Locke
SAY IT ISN''T SO!!! AAAAAAAGGGGGGGHHHHHH!!!!!! Gee, you live up to your nick =b hehehe

If this is the case, I ALMOST don''t want to use templates. Hmmm, what would happen if I placed the declaration and implementation in the same file and included THAT wherever it is needed? I hadn''t tried that. Then again, what would happen if I derived the class template in another file... this could get ugly. I''ll be trying this out when I get home later. I''ll be posting my progress with this problem so that others may be informed. Talk atcha''z later =b

- Jay
Quit screwin' around! - Brock Samson
quote:Original post by coderx75 Hmmm, what would happen if I placed the declaration and implementation in the same file and included THAT wherever it is needed? I hadn''t tried that. Then again, what would happen if I derived the class template in another file... this could get ugly.
I''m using this and it works just fine. Just put all the code in header file and include header as usual. What problems do you expect with derived classes? They work just fine.


You should never let your fears become the boundaries of your dreams.
You should never let your fears become the boundaries of your dreams.
quote:Original post by _DarkWIng_
I''m using this and it works just fine. Just put all the code in header file and include header as usual. What problems do you expect with derived classes? They work just fine.

Ah, there''s hope! If I have to keep all the code in the header file, does this mean I''ll have to keep all classes derived in that same header file too? I''m DYING to get outta work so I can get on this. I guess if I HAVE to put it all in one file I can live with it. =/
Quit screwin' around! - Brock Samson
In some way or another, you need to make sure that any code using your templates have access to the actual implementation code. Usually by just doing an #include with the .h file. I personally put the declaration and implementation together in one .h file. I've seen some people do the standard .h/.cpp thing, but then at the bottom of the .h, do an #include "something.cpp". Of course, it looks really odd to see a .cpp file included like a header, but it keeps your files organized at least.

If you derived more classes, I suppose you'd just have to include the .h that has all the declaration and implementation. I haven't bothered with inheritance and templates together, yet though, so I don't know.

Anyway, here's an example of how I manage it:

//TemplateExample.h#ifndef TE_H#define TE_Htemplate <class T>class TmpEx{  public:    TmpEx(const T& Value)    {      mValue = Value;    }    T Get()    {      return mValue;    }  protected:    T mValue;};#endif//Derived.h#ifndef D_H#define D_H#include "TemplateExample.h"template <class T>class TmpEx2 : public TmpEx<T>{  public:    TmpEx2(const T& Value) : TmpEx<T>(Value)    {    }    T Get()    {      return mValue + 2;    }};#endif//Main.cpp#include <stdio.h>#include "TemplateExample.h"#include "Derived.h"int main(){  TmpEx<int> A(5);  TmpEx2<int> B(6);  printf("%d, %d\n", A.Get(), B.Get());  return 0;}


[edited by - Agony on February 2, 2004 12:59:43 PM]
"We should have a great fewer disputes in the world if words were taken for what they are, the signs of our ideas only, and not for things themselves." - John Locke
Great! I''ll try that. Thanks, you guys been a huge help ;-) I''ll letcha know how things turn out.
Quit screwin' around! - Brock Samson
I think the other posters probably answered it, but here''s a nice write up on the problem and possible solutions.
http://sowell.ecs.fullerton.edu/cs331/TemplateClasses.html

BTW, VS.NET 2003 does not have this template problem.

Another link:
http://www.glenmccl.com/ansi_016.htm

Tadd
- WarbleWare
Tadd- WarbleWare
quote:Original post by reana1
BTW, VS.NET 2003 does not have this template problem.

Actually, it does have that "problem," and the reason why is because it''s a part of the language that the definition has to remain visible. The only way you are allowed to put the definition into a different translation unit and have it compile and link properly is to use the keyword export (which .NET 2003 doesn''t support).

The only compiler that currently supports the export keyword is the excellent comeau compiler

See also:
Export Restrictions Part 1
Export Restrictions Part 2

This topic is closed to new replies.

Advertisement