类定义错误 - 字段下一个具有不完整的类型
Class definition error - Field next has incomplete type
我有一个很小的问题。为什么此代码引发以下错误?
字段下一个具有不完整的类型。
我正在声明一个类,它有一个属性
#ifndef NODE_H
#define NODE_H
class Node
{
public:
int d;
Node(int d){
this->d = d;
}
Node next = 0;
};
#endif // NODE_H
但!!!如果我更改为指针有效:
Node *next;
很有趣,因为这来自破解编码采访的书。
有人可以对我有所了解吗?(一个很可能让我感到羞耻:D)
提前致谢
我做了功课,但在这里或这里都找不到解决方案
您正在声明一个类型 Node
的类,但该声明在到达右大括号和分号之前不完整。
class Foo
{
}; // Class declaration is complete now
但是,以下内容不起作用,请参阅代码注释。
class Node
{
public:
int d;
Node(int d){
this->d = d;
}
// This is a instance of a class and as such the compiler needs to
// know the full definition of the class. HOWEVER, this is the class
// that's being defined (i.e., it isn't fully defined yet!)
Node next = 0; // Assigning 0 here doesn't make any sense
};
指针不需要完整的但!!!如果我更改为指针工作
类型(即不需要完整的定义),因此可以在定义Node
类时使用。
这不是我得到的错误,那只是智能感知错误。
但是,Node
尚未完全定义,因此编译器不知道它是哪种对象,或者它占用了多少内存。
无论如何,我不认为这是问题所在。 在 Node
类本身中声明 Node
对象将创建递归依赖关系,同样,编译器将无法解析Node
需要多少内存。
由于指针只是一个内存地址并且具有固定的大小,因此编译器很高兴并且可以声明它。
相关文章:
- 类没有命名C++代码中的类型错误
- 不完整类型错误(E0409、E0070、E0515)
- 使用具有结构不完整类型错误的模板
- 不命名构造函数和析构函数上的类型错误
- 返回派生类型时出现协变类型错误
- 为什么此构造函数没有给出不完整的类型错误?
- 定义模板化结构的特征时出现不完整的类型错误
- 使用重载构造函数时出现不完整的类型错误
- 为什么我得到表达式必须有类类型错误?
- 输出 [left]=input[i] 行中的 c++ 代码中存在无效的类型错误
- 枚举成员不是类型错误
- 如何修复"ctypes"。参数错误:参数 2:<键入"异常.类型错误">:RaspberryPi 中的错误类型"错误
- 外部 "C" Visual Studio 2015 中的显式类型错误,DLL 测试代码
- 非默认析构函数会导致不完整的类型错误
- 为什么按引用传入会导致绑定引用类型错误
- Objective-C C++ 包装器 类型错误的不完整定义
- 输入类型错误
- 当构造函数的参数类型错误时引发异常
- 命名空间中的'bad_cast' 'std' 未命名类型错误
- C UNWORKOWN类型错误