user173342
user173342

Reputation: 1840

Implementing a member function of a class nested in a template class

I'm implementing template member functions in a .cpp, and understand the limitations involved (only going to template this class to one type at a time). Everything is working fine, except this one case. While I have gotten constructors for nested classes to work, I'm having trouble with any other nested class member function. For reference, I'm compiling with VC++ 11.

In .h:

template<typename T> class TemplateClass
{
 class NestedClass
 {
  NestedClass();

  bool operator<(const NestedClass& rhs) const;
 };
};

In .cpp:

//this compiles fine:
template<typename T>
TemplateClass<T>::NestedClass::NestedClass()
{
 //do stuff here
}

//this won't compile:
template<typename T>
bool TemplateClass<T>::NestedClass::operator<(const TemplateClass<T>::NestedClass& rhs) const
{
 //do stuff here

 return true;
}

template class TemplateClass<int>; //only using int specialization

edit: Here's the errors, all pointing to the operator< implementation line

error C2059: syntax error : 'const'
error C2065: 'rhs' : undeclared identifier
error C2072: 'TemplateClass::NestedClass::operator <' : initialization of a function
error C2470: 'TemplateClass::NestedClass::operator <' : looks like a function definition, but there is no parameter list; skipping apparent body
error C2988: unrecognizable template declaration/definition

Upvotes: 1

Views: 685

Answers (1)

Pubby
Pubby

Reputation: 53047

NestedClass is in scope as a parameter type and so you can try this:

 bool TemplateClass<T>::NestedClass::operator<(const NestedClass& rhs) const

MSVC is probably buggy on the first version.

Upvotes: 4

Related Questions