Sign in to follow this  
Fire Lancer

template class problem

Recommended Posts

Fire Lancer    189
template<typename T>class Outer
{
	public:
	template<typename T> class Inner//is this correct syntax?
	{
...
		bool operator == (const Outer<T>::Inner &Right)const;//what the correct syntax?
		bool operator != (const Outer<T>::Inner &Right)const;
	};
};




EDIT: warning C4346: dependent name is not a type prefix with 'typename' to indicate a type - ??? error C2061: syntax error : identifier 'Inner' [Edited by - Fire Lancer on August 13, 2008 8:20:04 AM]

Share this post


Link to post
Share on other sites
SiCrane    11839
That depends on what you're trying to do. Are you trying to create an Inner class that is parameterized independently of the Outer class? Is your Outer class actually named ItemList?

Share this post


Link to post
Share on other sites
Fire Lancer    189
No, I just copied it wrong, probobley account for one of the compile errors I thought was incorrect syntax/... (ItemList is actauly just a std::vector<entity::Base*> wrapper)

Inner should be a template class as well, but using the same template types as outter...

Share this post


Link to post
Share on other sites
SiCrane    11839
Then it sounds like you want:

template<typename T> class Outer {
public:
class Inner {
bool operator == (const Inner &Right) const;
bool operator != (const Inner &Right) const;
};
};

Share this post


Link to post
Share on other sites
Fire Lancer    189
ok, so now how do I actauly make the function?

template<typename T> bool Outer<T>::Inner::operator == (const Outer<T>::Inner&Right)
{
...
}



error C2065: 'Right' : undeclared identifier
warning C4346: 'Outer<T>::Inner' : dependent name is not a type
prefix with 'typename' to indicate a type
error C2470: 'Outer<T>::Inner::operator ==' : looks like a function definition, but there is no parameter list; skipping apparent body
error C2072: 'Outer<T>::Inner::operator ==' : initialization of a function

Share this post


Link to post
Share on other sites
SiCrane    11839
The second error message tells you that you should prefix Outer<T>::Inner with typename to indicate a type. Also, don't forget the const. So you'd have:

template<typename T>
bool Outer<T>::Inner::operator== (const typename Outer<T>::Inner & Right) const {

}

Share this post


Link to post
Share on other sites
SiCrane    11839
Quote:
Original post by SiCrane
The second error message tells you that you should prefix Outer<T>::Inner with typename to indicate a type. Also, don't forget the const. So you'd have:

template<typename T>
bool Outer<T>::Inner::operator== (const typename Outer<T>::Inner & Right) const {

}


Share this post


Link to post
Share on other sites
Have you tried in-lining your class-method definitions?
i.e.

template<typename T>class Outer
{
public:
template<typename T> class Inner//is this correct syntax?
{
bool operator == (const Outer<T>::Inner &Right)const
{
if(*this == Right) return true; else return false;
}
bool operator != (const Outer<T>::Inner &Right)const
{
//same deal etc...
}
};
};



Alternatively, for each different data type you use to instantiate your template class, you can append a line "template class Outer< insert types used here>;"

For example, if your project uses the Outer template for floats, ints, and bools, at the end of your Outer.cpp file you would add:
template class Outer<float>;
template class Outer<int>;
template class Outer<bool>;


Im not sure this is the problem youre having, but it looks like it from the rather vague error messages. Its a common problem caused by the way templates are compiled and linked, more info can be found here:
[url]http://www.parashift.com/c++-faq-lite/templates.html#faq-35.12[/url]

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this