简体   繁体   English

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

[英]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). 我正在.cpp中实现模板成员函数,并了解所涉及的限制(一次只能将此类模板化为一种类型)。 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. 作为参考,我正在使用VC ++ 11进行编译。

In .h: 在.h:

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

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

In .cpp: 在.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 编辑:这是错误,都指向operator<实现行

error C2059: syntax error : 'const' 错误C2059:语法错误:'const'
error C2065: 'rhs' : undeclared identifier 错误C2065:'rhs':未声明的标识符
error C2072: 'TemplateClass::NestedClass::operator <' : initialization of a function 错误C2072:'TemplateClass :: NestedClass :: operator <':函数的初始化
error C2470: 'TemplateClass::NestedClass::operator <' : looks like a function definition, but there is no parameter list; 错误C2470:'TemplateClass :: NestedClass :: operator <':看起来像一个函数定义,但是没有参数列表; skipping apparent body 跳过明显的身体
error C2988: unrecognizable template declaration/definition 错误C2988:无法识别的模板声明/定义

NestedClass is in scope as a parameter type and so you can try this: NestedClass作为参数类型在范围内,因此您可以尝试这样做:

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

MSVC is probably buggy on the first version. MSVC可能是第一个版本的bug。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM