c++ - 实现嵌套在模板类中的类的成员函数

标签 c++ templates visual-c++ compiler-errors

我正在 .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/

相关文章:

c++ - 通过 const 中间体访问非常量的设计模式

c++ - Qt5上的静态库 : Undefined symbols for architecture x86_64:

c++ - VBO : Array not drawn

c++ - 无法用 BER 解码 RSA 公钥

c++ - 类型检查模板类参数

c++ - 如何在其他模板类中专门化模板类?

c++ - 函数不读取和打印 C++ 中的文件内容

C++ 模板编程 : how to call type's function?

使用 visual studio 2008 构建 C++ 项目

c++ - MSVC2015 更新 3 可变参数模板解决方法