为什么编译器需要在已经类限定的成员函数定义的返回类型上使用类限定符
Why does the compiler require the class qualifier on the return type of an already class-qualified member function definition?
我有点不确定用什么措辞来描述这个问题,但举个例子:
Foo.h
namespace sample {
class Foo {
public:
enum Bar {
kValue1,
kValue2,
}
Bar SomeMethod(Bar some_value);
}
} // namespace sample
Foo.cc
namespace sample {
Bar Foo::SomeMethod(Bar some_value) { // compiler complains here
if (some_value == Bar::kValue1) {
return Bar::kValue2; // but not here
} else {
return Bar::kValue1;
}
}
} // namespace sample
编译器抱怨定义中的返回类型说:
错误:未知类型名称"Bar">
为什么在SomeMethod
的定义中赋予限定Foo::SomeMethod
并没有将相同的限定扩展到Bar
的返回类型,而是将限定扩展到该方法定义中Bar
的所有其他用途?
Bar Foo::SomeMethod (Bar some_value)
//^^^ Here is "outside" the class ^^^ Here is "inside" the class
由于Bar
是在类中定义的,因此需要使用Foo::
前缀来访问它,除非您在类本身内部。
这是一个名称查找问题。通常,直到您进入参数列表或函数体:,您才能自动看到Foo
的内部
namespace sample {
class Foo {
public:
enum Bar {
kValue1,
kValue2,
};
Bar SomeMethod(Bar some_value);
};
Foo::Bar Foo::SomeMethod(Bar some_value) {
if (some_value == Bar::kValue1) {
return Bar::kValue2;
} else {
return Bar::kValue1;
}
}
} //namespace sample
int main() {}
相关文章:
- 在没有定义返回类型的函数中返回布尔值,并将结果保存在无错误的char编译中-为什么
- QtQuick - qml:28:错误:未知方法返回类型:自定义类型
- 如何在C++中允许成员函数的自定义返回类型进行类型擦除?
- 如何在 Cython 中定义返回 cpp 定义类型的函数?
- 用户定义的转换无法指定返回类型
- 将成员函数的返回类型引用到C++中的自定义类
- 如何定义依赖于参数包转换的函数的返回类型
- 在类定义中推导内联静态成员函数的返回类型
- 如果函数是在类的主体中定义的,我是否需要在成员函数的返回类型中指定 typename?
- 从 QAbstractItemModel 返回自定义用户类型
- 为什么用户定义的转换函数模板不能有推导的返回类型?
- 在Cython中定义C 结构返回类型
- 为什么在.h中定义的私有结构需要.cpp文件中的返回类型中的范围
- 函数返回自定义的托管类类型
- 是否可以为类设置自定义返回类型
- 具有自定义返回类型和"false"返回条件的函数?
- C++:如何使用泛型返回类型定义虚函数
- 为什么我不能返回类型定义的引用
- 为什么我不能用非引用返回类型定义运算符=?
- C++类函数返回类型定义,返回一个类