Jump to content
  • Advertisement

Archived

This topic is now archived and is closed to further replies.

Wavarian

Templates: What is wrong with this?

This topic is 5292 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

Just now I''ve run into a small error, which is better explained with the following code:
template <class T,typename K>
class CBase{

     typedef CBase<T,K> CB_obj;
     ...

public:

     ...
     CB_obj* GetObject(K key);
};

template <class T,typename K>
typedef CBase<T,K> CB_obj;

template <class T,typename K>
CB_obj* CBase<T,K>::GetObject(K key){...}
The above does not compile, giving me an error that the definition of GetObject() and the implementation of GetObject() differ only by return type. I know it could very well have something to do with the second typedef call, however, I am unsure, hence my reason to write this post. The below code does compile, and this is a work around, but I''d really like to keep the above.
template <class T,typename K>
class CBase{

     typedef CBase<T,K> CB_obj;
     ...

public:

     ...
     CBase<T,K>* GetObject(K key);
};

template <class T,typename K>
typedef CBase<T,K> CB_obj;

template <class T,typename K>
CBase<T,K>* CBase<T,K>::GetObject(K key){...}
Any help on this would be appreciated. *Keeps his eye on the horizon for SiCrane* O.O

Share this post


Link to post
Share on other sites
Advertisement
Because you have 2 different CB_obj types. One as CBase::CB_obj and one as ::CB_obj. Try placing a forward declaration of CBase and the typedef before the definition of CBase and see if that works for you (and lose the typedef inside CBase).


[edited by - joanusdmentia on January 24, 2004 3:51:54 AM]

Share this post


Link to post
Share on other sites
Thanks for the reply, however I tried that too earlier, and ended up with some strange msvc error.



template <class T,typename K>
class CBase;
//

template <class T,typename K>
typedef CBase<T,K> CB_obj;
//

template <class T,typename K>
class CBase{
...
};


^-- That gave me something whacked out.[/source]

[edited by - wavarian on January 24, 2004 3:52:43 AM]

Share this post


Link to post
Share on other sites
How about the following


template <class T,typename K>
class CBase{

public:

...
CBase* GetObject(K key);
};

template <class T,typename K>
typedef CBase<T,K> CB_obj;

template <class T,typename K>
CB_obj* CBase<T,K>::GetObject(K key){...}


Or alternatively giveup on the typedef and also make life a little bit easier with:


template <class T,typename K>
class CBase{

public:

...
CBase* GetObject(K key);
};

#define CB_obj CBase<T,K>

template <class T,typename K>
CB_obj* CB_obj::GetObject(K key){...}


[edited by - joanusdmentia on January 24, 2004 3:56:03 AM]

Share this post


Link to post
Share on other sites
You cannot have a templated typedef. Probably the simplest solution to your problem is to do:
template <class T,typename K>
class CBase
{
public:
typedef CBase<T,K> CB_obj;
CB_obj* GetObject(K key);
};

template <class T,typename K>
CBase<T,K>::CB_obj* CBase<T,K>::GetObject(K key)
{
}


Enigma

EDIT: chomped my < & >'s

[edited by - Enigma on January 24, 2004 6:04:27 AM]

Share this post


Link to post
Share on other sites

  • Advertisement
×

Important Information

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

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!