Jump to content
  • Advertisement
Sign in to follow this  
blitzkrieger

using user-defined data types with templates

This topic is 4870 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hello. I want to use a simple template class (game_object_manager) with my user-defined type (billard_sphere). My user-defined type is a class that has a user-defined constructor and some public member-functions. If i try to instantiate the template class with my user-defined class as type i get a compiler-error. To see Code-Snippets, please scroll down. QUESTIONS: 1. How and where should i best declare my user-defined type (billard_sphere) so that the instantiation in main.cpp works? 2. In billard_sphere, if any, what operators do i need to overload in order to use it with the template class? 3. Do you have any general hints regarding the issue? Thank you for all answers. CODE-SNIPPETS: //-----------------memtest.h :
template <class GameObjectType>
class game_object_manager
{
private:
   vector<class GameObjectType> v_Objects ;

   int ObjectNumber ;
   int CurrentObject ;

public:
   game_object_manager() ;
   ~game_object_manager() ;

   void New_Object(void) ;
   // other member-functions skipped
} ;
//-----------------memtest.cpp :
#include<vector>
using namespace std ;

#include"memtest.h"

template <class GameObjectType>
void game_object_manager<GameObjectType>::New_Object(void) 
{
   GameObjectType NewObject ;
   this->v_Objects.push_back(NewObject) ;
}
// other member-function-definitions skipped

// explicit instantiation:
template game_object_manager<billard_sphere> ;
//-----------------sphere.h:
class billard_sphere 
{
private:

   float X,Y,Z,V ;
   float VX,VY,VZ ;
   float Masse,Radius ;

public:
   billard_sphere(float X, float Y, float Z, float Mass, float Radius) ;
   billard_sphere(void) ;
   ~billard_sphere(void) ;
	
   void SetPosition(float X, float Y, float Z) ;
   // other member-functions skipped
} ;
//-----------------sphere.cpp:
#include"sphere.h"

void billard_sphere::SetPosition(float X, float Y, float Z) 
{
   this->X = X ; this->Y = Y ; this->Z = Z ;
}
// other member-function-definitions skipped
//-----------------main.cpp:
#include"memtest.h"
#include"billard_sphere.h"

// inside main-function (function-body skipped) 
game_object_manager<billard_sphere> Fahrzeuge ;

Added source blocks -- Fruny. [Edited by - Fruny on February 16, 2005 8:24:17 AM]

Share this post


Link to post
Share on other sites
Advertisement
You have to put the template code inside the .h file where it's defined, instead of a .cpp file.

If you insist on having two files, instead of a .cpp, use some other extension, e.g., .inl, and do something like this:

//-----------------memtest.h :

template <class GameObjectType>
class game_object_manager
{
...
};
//Include the file with the implementation of game_object_manager, formerly
//known as memtest.cpp.
#include "memtest.inl"


Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!