Skip to main content
gamedev.net
🔒 Locked

Template Classes

Started by NuffSaid 6 replies 400+ views
Original Post
NuffSaid
NuffSaid
I''m having trouble with template classes under MSVC++. I seem to have to declare and define all the member functions of the class in the header file if I''m going to use templates. Is there something I''m doing wrong?
  
template <class T>
class A
{
protected:
  T *data;
public:
  A() : data(NULL){/*Do nothing*/}
  virtual ~A(){}
  void setData(T *newData);//I have to define this in the class

                           //definition and I can''t do this in a 

                           //.cpp file.

};

  
And another thing. A template class that compiles fine on MSVC++ doesn''t compile at all on g++. Is there some specific switch that I must use or does g++ not support templates well (I remember reading somewhere...).
==========================================In a team, you either lead, follow or GET OUT OF THE WAY.
Icarus3
Icarus3
If you want to use it in the cpp file you have to do this:

      

// In the header


template <class T>
class A
{
protected:
T *data;
public:
A() : data(NULL) {/*Do nothing*/}
virtual ~A(){}
void setData(T *newData);
};

         
// in the cpp file

#include "blaat.h"

template <class T>
A::setData(T* newData)
{
........
}


So for every function you want to implement you have to put template above it

Edited by - Icarus3 on October 7, 2001 4:52:14 PM
NuffSaid
NuffSaid
I''ve tried what you showed. It doesn''t work. I get compiler errors.

Here are some of the stuff I''ve done

  
//in the cpp file


//1. Doesn''t work

template <class T>
void A::setData(T *data)
{
}


//2. Doesn''t work either

template <class T>
void A<T>::setData(T *data)
{
}


//3. Doesn''t work and I was getting desperate :)

template <class T>
void A<T>::setData<T>(T *data)
{
}

==========================================In a team, you either lead, follow or GET OUT OF THE WAY.
Dactylos
Dactylos
The reason it doesn''t work is because MSVC++ can''t handle separately compiled templates. That''s why you have to specify the function/method bodies in the header-files.

GCC/g++ generally has better template support than MSVC++ (unless you''re using a really old version of g++), IIRC g++ can even compile templates separately.
NuffSaid
NuffSaid
So, it is MSVC++''s fault then. Crummy compiler. Wonder if the new version fixes it....

p.s. No, I''m not switching to Borland just to get templates to work. Just a personal opinion but I dislike the interface.
==========================================In a team, you either lead, follow or GET OUT OF THE WAY.
donbonifacio
donbonifacio
you can not define template classes in cpp files, all template classes must be defined inline!!!
if you wanto to check, try opening the STL dir, you will see that there is no cpp...

g++ compiles templates well, VC++ don''t! there are many problems in VC++ because they didn''t went standard there... if you can not compile a template under g++, maybe there''s some parameters missing when you call the g++ (..) what do you write to compile?

about your code, you will have to do this:

template
class A{
protected:
T *data;
public:
A() : data(NULL){/*Do nothing*/}
virtual ~A(){}
void setData(T*newData);
};

template
void A::setData( T *data )
{
/* your code here */
}
Shannon Barber
Shannon Barber
The only thing I''ve heard of that MSVC6 doesn''t support is partial template specialization.

Anyway, templates have to be defined inlined. They don''t produce any compilable code until you use them on something. I generally put template classes in a .hpp file; the MS STL files just don''t have an extention.

Magmai Kai Holmlor
- Not For Rent
The trade-off between price and quality does not exist in Japan. Rather, the idea that high quality brings on cost reduction is widely accepted.-- Tajima & Matsubara

Topic Locked

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

Sign in to reply to this topic.