Sign in to follow this  
blitzkrieger

using user-defined data types with templates

Recommended Posts

blitzkrieger    122
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
Couvillion    160
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

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this