我正在 .cpp 中实现模板成员函数,并了解所涉及的限制(一次只能将此类模板化为一种类型)。一切正常,除了这种情况。虽然我已经获得了嵌套类的构造函数,但我在使用任何其他嵌套类成员函数时遇到了麻烦。作为引用,我正在使用 VC++ 11 进行编译。
在 .h 中:
template<typename T> class TemplateClass
{
class NestedClass
{
NestedClass();
bool operator<(const NestedClass& rhs) const;
};
};
在 .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
编辑:这是错误,都指向 operator<
实现线
错误 C2059:语法错误:'const'
错误 C2065:“rhs”:未声明的标识符
错误 C2072:“TemplateClass::NestedClass::operator <”:函数初始化
error C2470: 'TemplateClass::NestedClass::operator <' : 看起来像函数定义,但没有参数列表;跳过明显的 body
error C2988: 无法识别的模板声明/定义
最佳答案
NestedClass
作为参数类型在范围内,因此您可以试试这个:
bool TemplateClass<T>::NestedClass::operator<(const NestedClass& rhs) const
MSVC 的第一个版本可能有问题。
关于c++ - 实现嵌套在模板类中的类的成员函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14550270/