结构的前向声明无效
invalid forward declaration of struct
-编辑--很抱歉,我把人们弄糊涂了,我只是快速地键入了这段代码,而不是复制和粘贴,所以我实际上在代码中定义了#ifndef A_H#。我更改了下面的代码以显示
--结束编辑--
我有两个类,每个类都包含一个指向另一个类实例的指针,但这给我带来了问题。我的代码类似于下面的
// A.h
#ifndef A_H
#define A_H
class B; // compiler error here
class A
{
B* foo;
// other members and functions
};
#endif
// A.cpp
#include "A.h"
#include "B.h"
/*
declare functions and use methods in both A and B
*/
// B.h
#ifndef B_H
#define B_H
class A;
class B
{
A** bar;
// other stuff
};
#endif
//B.cpp
#include "A.h"
#include "B.h"
/*
declare functions and use methods in both A and B
*/
有人告诉我,在头文件中向前声明另一个类,然后将另一个文件包括在cpp文件中是可行的,但在标记的行中,我得到了一个错误,只说"向前声明‘struct b’"
有人能告诉我我做错了什么吗?
包括一个标头,比如说a.h中的b.h。不要在a.h中转发声明b。b.h可以保持原样。
否则你会得到类似的东西
class B {};
....
class B;
只对这样的错误进行预处理总是明智的。
相关文章:
- 声明为无效的变量或字段'...' Ardunio 编译器上的错误
- '{'标记之前的预期类名,然后在预声明时无效使用不完整的类型'class class_name'
- 为什么这种向量声明无效?
- 错误:变量或字段'PrintEntity'声明无效无效打印实体(实体 e);
- 在另一个结构中声明内部结构会导致错误:结构使用无效
- 类外的前向声明有效,但在嵌套时无效
- C++ SPDLOG 编译错误:变量或字段"set_error_handler"声明为无效
- 使用无效指针初始化指针声明符的行为是否未定义?
- 书籍示例在类声明中使用"explicit"关键字。无效吗?
- 将前向声明的类型转换为无效是否合法?
- 最终说明符对单独的声明和定义无效
- 没有变量声明为函数,但错误:二进制表达式的操作数无效
- 声明是否无效()合法,它实际上是什么?
- 声明为无效的变量或字段'name of var'
- 错误:非静态数据成员的使用无效,在此范围内未声明变量
- 声明std ::配对时大小的写入无效
- 如何使用Auto声明无效指针
- 结构的前向声明无效
- 转发声明无效
- C++使函数声明无效