嵌套名称说明符中的不完整类型
Incomplete type in nested name specifier
我尝试在嵌套名称说明符中使用不完整类型,如下所示:
class A;
int b= A::c; // error: incomplete type ‘A’ used in nested name specifier
class A {
static const int c=5;
};
在N3797工作草案的3.4.3/1中没有提到这一点:
可以引用类或命名空间成员或枚举数的名称在::范围解析操作符(5.1)之后嵌套名称说明符,表示其类、名称空间或枚举
行为实现依赖吗?
简介
在标准中有几个地方暗示你的代码是病态的,但是下面的引用说明了一切:
你的代码的问题不在于你试图到达不完整类型的内部,问题在于你只能在声明之后引用类成员名。
3.3.2p6
声明点[basic.scope.pdecl]
在类成员声明点之后,可以在其类的作用域内查找成员名。
由于你的前向声明(当然)没有引入任何名为c的成员,引用这样的名称是错误的。
误导性诊断…
gcc和clang在输入你的代码时发出的诊断有些误导,老实说,我觉得一个bug报告是合适的。
foo.cpp:3:8: error: incomplete type 'A' named in nested name specifier
允许在嵌套名称说明符中命名不完整类型,但是如上所述;不允许引用尚未声明的成员。
不规范的:
class X {
static int a[X::x]; // ill-formed, `X::x` has not yet been declared
static int const x = 123;
};
法律:class X {
int const x = 123;
int a[X::x]; // legal, `X` is incomplete (since we are still defining it)
// but we can still refer to a _declared_ member of it
};
当访问一个类的成员变量时,我得到了相同的错误,该成员变量已在头文件中向前声明,但未包含在我访问它的主程序中。
myClass.h
class FowardInclude; // has member "int x", but only forward-declared here
class MyClass {
public:
void foo(int num);
}
main.cpp
#include "forwardInclude.h" // <-- This is the line I was missing
#include "myClass.h"
MyClass m;
m.foo(ForwardInclude::x);
相关文章:
- 缺少类型说明符,显式类型为"缺少错误"
- 与 tesseract::TessBaseApi() 相关的 Tesseract-OCR 出错(预期的类型说明符)
- 为什么转换函数声明不需要至少一个定义类型说明符
- C++需要所有声明的类型说明符和预期的非限定 id(需要调试帮助)
- printf 数据类型说明符复杂问题
- C++错误:"class name"之前的预期类型说明符
- 缺少类型说明符和另一个问题
- 缺少类型说明符 - 假定为 int.注意 c++ 不支持 default-int
- 错误:C4430:缺少类型说明符-假定为int.注意:C++不支持default-int
- 在C++中出现错误之前,无效使用了不完整的类型和所需的类型说明符
- 缺少类型说明符 - int 假定 声明静态成员后出错
- 不能在类型说明符中定义联合
- 没有带有字符串数组的存储类或类型说明符
- 警告:函数使用不带尾随返回类型的'auto'类型说明符
- 仅为某些模板专用化定义转换运算符:预期类型/预期类型说明符
- 在 C++ 中出现unique_ptr错误之前的预期类型说明符
- 有没有办法将类型说明符存储在变量中
- C 期望类对象的类型说明符
- 是无符号的int"简单类型说明符"
- 在 vscode 上编译错误.类型说明符是 C++11 扩展