template class problem

Started by
7 comments, last by WhatsUnderThere 15 years, 8 months ago

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]
Advertisement
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?
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...
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;    };};
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
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 {}
ok, but now I get an unable to match error...

template<typename T> bool Outer<T>::Inner::operator == (const typename Outer<T>::Inner &Right)
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 {}


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:
http://www.parashift.com/c++-faq-lite/templates.html#faq-35.12

This topic is closed to new replies.

Advertisement