Archived

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

Wavarian

Templates: What is wrong with this?

Recommended Posts

Wavarian    850
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
joanusdmentia    1060
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
Wavarian    850
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
joanusdmentia    1060
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
Enigma    1410
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