继承类时"invalid use of incomplete type ‘class tree_node_t’"
"invalid use of incomplete type ‘class tree_node_t’" when inheriting class
node.h:
#include "tree_node.h"
class tree_node_t;
class node_base : public tree_node_t
{
public:
node_base();
~node_base();
};
tree_node.h:
#include "node.h"
class node_base;
class tree_node_t
{
tree_node_t();
std::vector<node_base*> list;
. . .
}
一切看起来都是正确的,但由于某种原因,我收到错误"无效使用不完整的类型'类tree_node_t'"。我不明白为什么会这样。
据我了解,这些问题的解决方案是将文件拆分为标头(.h(和源代码文件(.cpp(。分裂,但继续收到这样的错误。
不能从不完整的类型继承。编译器在从tree_node_t
继承时需要具有完整的定义,以便正确确定类的大小以及数据成员的偏移量。
但是,通过在已经包含标头之后放置class tree_node_t;
,您可以隐藏编译器所需的定义。因此,只需删除行class tree_node_t;
应该可以使每个编译都很好,除非您还缺少包含保护。
正如Rudolfs Bundulis正确指出的那样,您还需要删除tree_node.h
中的#include "node.h"
,否则在包含tree_node.h
时传递给编译器的代码如下所示:
class node_base : public tree_node_t
{
public:
node_base();
~node_base();
};
class node_base;
class tree_node_t
{
tree_node_t();
std::vector<node_base*> list;
. . .
};
它无法编译,因为突然之间,tree_node_t
的定义出现在试图从它继承的node_base
的定义之后。
natersoz和Markus是对的。我所要做的就是从tree_node.h
中删除#include
。
据我了解,解决这些问题的方法是拆分 文件转换为标头 (.h( 和源代码文件 (.cpp(。
将代码拆分为文件通常是首选解决方案。 这至少对你来说是一件对你有益的事情。
但是,通常仍然可以在单个文件中执行此代码,在这种情况下,可以使用一个前向声明。
以下编译、链接和运行(但作用很小(:
#include <iostream>
using std::cout, std::flush, std::endl;
#include <vector>
using std::vector;
// in this ordering, the single forward suffices,
// because all class pointers are a known size,
// and
// this symbol (as yet) is only in the tree_node_t data,
// in the vector as a pointer
class node_base_t;
class tree_node_t
{
// private data attributes
vector<node_base_t*> list;
// tbd ... other data
public:
tree_node_t() // default ctor
// : list ctor is ok
{
cout << "n tree_node_t() " << flush;
}
~tree_node_t() = default; // dtor
// tbd ... more public functions
private:
// tbd ... private functions
};
class node_base_t : public tree_node_t
{
// private data
// ... tbd - other data
public:
node_base_t() // tree_node_t default ctor is ok
{
cout << "n node_base_t() " << flush;
}
~node_base_t() = default;
// tbd ... more public functions
private:
// tbd ... private functions
};
int main(int , char** )
{
int retVal = -1;
{
node_base_t nb;
retVal = 0;
}
cout << "n" << endl;
return retVal;
}
输出:
tree_node_t()
node_base_t()
相关文章:
- 这个语法std::class<>{}(arg1, arg2) 在C++中是什么意思?
- C++核心准则 C35 对于接口类"A base class destructor should be either public and virtual, or protected and nonv
- 使用模板进行堆栈实现; "name followed by :: must be a class or namespace"
- "Undefined class"作为is_base_of的论据
- 如何将class.cpp和class.hpp编译为一个.o文件
- '{'标记之前的预期类名,然后在预声明时无效使用不完整的类型'class class_name'
- 如何使用"derived input class"创建派生类?
- 错误:"Left of getValue must have class/struct/union"
- ' class a : b ' 和 ' class a : public b ' 之间的继承类不同
- Pybind11 Class Definition
- 在C++中使用没有合作伙伴Class/Cpp文件的头文件是否实用
- C2011 'CMemDC':Visual Studio 2019中的'class'类型重新定义
- TypeError: [c++ addon class] 不是构造函数
- C++ class template
- 当初始值设定项是基类名时'initializer does not name a non-static data member or base class'错误
- Clearing Class Foo with new(pFoo) Foo()
- 使用 boost::p roperty_tree::p tree 如何获取特定键的值
- C++ class vs a library
- 继承类时"invalid use of incomplete type ‘class tree_node_t’"
- 使用"class"关键字,后跟未声明的标识符