Advertisement Jump to content
Sign in to follow this  
Lord Gzoo

templates, nested classes, and C++

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

hi. i am doing something terribly wrong. let the code speak:
template<class T>
class Crocodile{
	public:
		class Nested{
			public:
				Nested(int) {};
		};

	void func( Nested& n ); /* non-inline and works fine */
	
	Nested func2(){ /* inline, and works fine, notice: returns nested class */
		cout << "your crocodile is big\n";
		return Nested(15);
	}
	
	Nested func3(); /* same as func2, only i want it as non-inline */
};

template<class T>
void Crocodile<T>::func( Crocodile<T>::Nested& n ){
/* happy joy, it accepts Crocodile<T>::Nested as an argument */
	cout << "cheers~\n";
}

template<class T>
Crocodile<T>::Nested Crocodile<T>::func3(){
/* no happiness nor joyness */
	cout << "big crocodile eats you all day long\n";
/*do something
return something*/
}

in the func3() definition, the compiler (dev-c++ with the default mingw provided with it) refuses to believe that Crocodile<T>::Nested is an legal type and it produces all kinds of funky errors.

Share this post


Link to post
Share on other sites
Advertisement
The compiler isn't willing to trust you that Crocodile<T>::Nested will refer to a class for any T, since there might be a specialization like this:

template<> class Crocodile<int> { static double Nested; }

Solution: use the "typename" keyword in front of things which you wish to reassure the compiler are really type names.

Share this post


Link to post
Share on other sites
ex:

template<class T>
void Crocodile<T>::func( typename Crocodile<T>::Nested& n ){
/* happy joy, it accepts Crocodile<T>::Nested as an argument */
cout << "cheers~\n";
}

template<class T>
typename Crocodile<T>::Nested Crocodile<T>::func3(){
/* no happiness nor joyness */
cout << "big crocodile eats you all day long\n";
/*do something
return something*/
}

Share this post


Link to post
Share on other sites
Try adding a couple of typename's, as below.


template<class T>
void Crocodile<T>::func( typename Crocodile<T>::Nested& n ){
/* happy joy, it accepts Crocodile<T>::Nested as an argument */
cout << "cheers~\n";
}

template<class T>
typename Crocodile<T>::Nested Crocodile<T>::func3(){
/* no happiness nor joyness */
cout << "big crocodile eats you all day long\n";
/*do something
return something*/

}




EDIT: Sloow.

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.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!