gpt4 book ai didi

c++ - 子类化的 STL list::reference 编译错误

转载 作者:行者123 更新时间:2023-11-30 04:29:14 25 4
gpt4 key购买 nike

我正在将一个项目从 Visual Studio 6 转换到 Visual Studio 2010。该项目大量使用 STL。有一个自定义类,其子类列表可以向其添加更多方法。以下是我得到的代码和编译器错误。

    #include <list> 
namespace mySpace
{
template <class T>
class MyList : public std::list<T>
{
...
};

template <class T>
MyList<T>::reference MyList<T>::find(const_reference p_constreferenceItem) const
{
return std::find(this->begin(), this->end(), p_constreferenceItem);
}
} // namespace mySpace

错误:

Error   2   error C2143: syntax error : missing ';' before 'MySpace::MyList<T>::find'   c:\myproject\mylist.h   300 1
Error 3 error C4430: missing type specifier - int assumed. Note: C++ does not support default-int c:\myproject\mylist.h 300 1
Error 4 error C2888: 'MyList<T>::reference reference' : symbol cannot be defined within namespace 'MySpace' c:\myproject\mylist.h 300 1

知道是什么导致了这个错误吗?

最佳答案

您似乎只是缺少一个 typenameMyList<T>::reference 前,因为它是一个依赖类型(依赖于 T 的类型)并且编译器不知道 MyList<T>::reference是一种类型,默认情况下假定它是一个变量名:

template <class T>
typename MyList<T>::reference MyList<T>::find(
const_reference p_constreferenceItem) const

const_reference 不需要这个不过,因为编译器在 MyList<T> 中他一看到 :: 的范围。 (方法名称)现在知道 const_reference命名一个类型。以同样的方式你不需要使用 typename当你已经在 MyList<T> 里面时的范围,例如在类定义中声明或内联定义方法时。

不过,我不确定为什么 VC6 允许这样做。但是我们都知道 VC6 和模板并不是那么好的 friend ,更不用说当时 C++ 模板及其行为的松散规范。

免责声明:如果此处使用的术语有点非正式,请向语言律师道歉。

关于c++ - 子类化的 STL list<T>::reference 编译错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9533726/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com