Jump to content
  • Advertisement
Sign in to follow this  
MumbleFuzz

Separating template implementations (minor detail)

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

I'm trying to separate a template implementation from the header file by including it in a .inl file. Everything works, but I don't really understand this... Here's a simple templated class:
template<class ObjType>
class TplClass {
public:
	typedef std::list<ObjType*> ObjList;
	void Add( ObjType* obj );
	ObjList* GetObjectList();
private:
	ObjList m_objList;
};
With the following implementation:
template<class ObjType>
void TplClass<ObjType>::Add( ObjType* obj ) {
	m_objList.push_back( obj );
};

template<class ObjType>
std::list<ObjType*>* TplClass<ObjType>::GetObjectList() {
	std::list<ObjType*>* objList = new std::list<ObjType*>;
	
	std::list<ObjType*>::iterator objItr = m_objList.begin();
	for ( ; objItr != m_objList.end(); ++objItr ) {
		objList->push_back( *objItr );
	};

	return objList;
};
This works, but changing the GetObjectList function to the code below doesn't, and I was wondering why.
template<class ObjType>
TplClass<ObjType>::ObjList* TplClass<ObjType>::GetObjectList() {
...
};
Thanks in advance.

Share this post


Link to post
Share on other sites
Advertisement
The compiler can't know whether TplClass<ObjType>::ObjList is a static member or a typedef until it knows what ObjType is. By default it assumes it is a static member. Use the typename keyword to let the compiler know that it is a typedef:
typename TplClass<ObjType>::ObjList* TplClass<ObjType>::GetObjectList() {

...

};

Enigma

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.

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!