1
votes

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

1
What is the error (if any)? - 0x499602D2
@David Added my errors. This is in VC++ 11, if it makes any difference. - user173342
@user173342, it does make a difference. My sympathies. - StoryTeller - Unslander Monica
@user173342 FYI NestedClass is in scope as a parameter type. Try bool TemplateClass<T>::NestedClass::operator<(const NestedClass& rhs) const - Pubby
@Pubby You got it! Turn that into an answer. - user173342

1 Answers

4
votes

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.