簡體   English   中英

實現嵌套在模板類中的類的成員函數

[英]Implementing a member function of a class nested in a template class

我正在.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 <':函數的初始化
錯誤C2470:'TemplateClass :: NestedClass :: operator <':看起來像一個函數定義,但是沒有參數列表; 跳過明顯的身體
錯誤C2988:無法識別的模板聲明/定義

NestedClass作為參數類型在范圍內,因此您可以嘗試這樣做:

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

MSVC可能是第一個版本的bug。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM