Not logged in, Join Here! or Log In Below:  
News Articles Search    

 Home / General Programming / Templated classes in library C++ Account Manager
Archive Notice: This thread is old and no longer active. It is here for reference purposes. This thread was created on an older version of the flipcode forums, before the site closed in 2005. Please keep that in mind as you view this thread, as many of the topics and opinions may be outdated.

April 07, 2005, 07:14 PM

SOME of my code ( templated ) is being generated and placed in my .lib while some is not.

( forgive me, the terminology needed escapes me ) In this post I am going to say the same thing 3 different ways hopefully my blather will get my problem across... sorry.

-I wrote a game engine which utilizes templated singletons in several places.
-The main GAME ENGINE class ( the entry point ) is a singleton ( templated )

Everything works fine...

THEN... I moved the engine code into a library...

I included .lib in my project but it does NOT link... says the .instance() member off one of my classes is unresolved.

The class in question is the GAME ENGINE class... all others have needed code.

So, I added this to the end of my .lib code

  2. class dummy_class  
  3.   {
  4.     public:
  5.       MyClass_Singleton* Test;            // the only templated
  6.                                                // singleton class
  7.                                                // that DIDN'T work
  8.                                                // *see below in post
  10.     dummy_class() { Test = Test->instance(); };
  11.     ~dummy_class() {};
  12.   };
  14. dummy_class dummy_obj;    // this code does the job.
  16. // by not including the above line of code
  17. // the .instance() member of MyClass_Singleton is not
  18. // generated as part of the .lib
  20. // MyClass_Singleton is templated...
  22. // other classes using the same template are generated correctly.
  23. // NOTE:  This is because somewhere in the .lib code the class
  24. // in question is USED.

With this addition code my missing .instance() now exists.

Okay fine I got it to work, but is a better method available?

This is my first shot at a .lib and I am fairly new to templates.



My GAME ENGINE class appears to be the only one with this problem, becuase it is the only one which is never actually USED in the library code. The other ( templated singleton classes ) are used somewhere as part of the GAME ENGINE class.

The .lib user ( app writer ) is the person responsible for creating the first instance of the GAME ENGINE obj...

So, just defining a class from a template is not enough? You must USE the class in another for the compiler to generate the code in a .lib?


I realize since my GAME ENGINE IS a singleton my solution shouldn't matter... the app writer is going to CREATE one instance of the GAME ENGINE anyway! ( I have given he/she no alternative ) BUT i would like to leave WHEN it's created up to the app writer!

NOTE: the GAME ENGINE is not going to be a singleton for long... I plan on making it an ordinary class... but I am still interested in why, what & how is going on here AND is there a workaround.




April 08, 2005, 07:20 AM

Okay... I narrowed down the terminology...

template instantiation is the headache.

'In C++ unreferenced template functions should not be compiled...'

So my .instance() member is never generated...

So, I need to find a way to explicitly do so... I did find numerous sources on the subject but nothing indicating if "I should or should not" head down this road...

Does the need to control template instantiation indicate the need for an improved design?

If I must use explicit template instantiation or the like... I do NOT want a compiler specific solution...

I am totally new to this subject... looking for an opinions / insights.

-Borland Builder 6...


This thread contains 2 messages.
Hosting by Solid Eight Studios, maker of PhotoTangler Collage Maker.