c++前置声明用于解决:c++编译遇到问题error C2143: syntax error : missing ';' before '*'

本文详细解析了C++编程中头文件循环包含导致的编译错误,如missing typespecifier和C2061错误,并提供了解决方案。通过在头文件中使用前置声明代替相互包含,避免了编译时的死循环。

转自:https://blog.youkuaiyun.com/liunan199481/article/details/82585312
这个错误还可能导致以下错误:
error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
error C2061
查阅文档msdn发现:
在这里插入图片描述

  • 导致这种错误的可能有一下一些情况:
  • 在不该加分号的地方加分号
  • 在该加分号的地方不加分号
  • 头文件循环包含

这边就讲一下我遇到的第三个情况
我在helper.h头文件中定义了一个类,它其中又要用到imp.h中的类,而imp.h中又用到helper.h中的一个类,如果是这种情况,我们头文件包含这样写:
//helper.h
#include “imp.h”
…blablabla…
//imp.h
#include “helper.h”
…blabla…
这样的话就会造成上面的错误
这是上面原因呢?
因为编译的时候,编译到helper.h,它就要去找imp.h包含进来,这是要编译imp.h就要找helper.h包含进来,这样就会实现一个死循环,编译失败,造成上面的错误。
怎么解决呢
在某一个头文件中申明要用的类
比如在imp.h中实际上要用到helper.h中的类是test类,我们应该这样做,使用前置声明占位:
//imp.h
//不用include helper.h了
class test;
//imp.cpp
//在cpp文件中include
#include “helper.h”
在imp.h中声明一下,就可用,不用包含头文件,在真正需要包含的cpp文件中包含即可。

Compiling... 实验2.c C:\Users\Administrator\Desktop\数据结构实验报告\test\实验2.c(32) : error C2275: 'Node' : illegal use of this type as an expression C:\Users\Administrator\Desktop\数据结构实验报告\test\实验2.c(8) : see declaration of 'Node' C:\Users\Administrator\Desktop\数据结构实验报告\test\实验2.c(32) : error C2065: 'newNode' : undeclared identifier C:\Users\Administrator\Desktop\数据结构实验报告\test\实验2.c(34) : error C2223: left of '->data' must point to struct/union C:\Users\Administrator\Desktop\数据结构实验报告\test\实验2.c(37) : error C2223: left of '->next' must point to struct/union C:\Users\Administrator\Desktop\数据结构实验报告\test\实验2.c(38) : warning C4047: '=' : 'struct Node *' differs in levels of indirection from 'int ' C:\Users\Administrator\Desktop\数据结构实验报告\test\实验2.c(42) : error C2275: 'Node' : illegal use of this type as an expression C:\Users\Administrator\Desktop\数据结构实验报告\test\实验2.c(8) : see declaration of 'Node' C:\Users\Administrator\Desktop\数据结构实验报告\test\实验2.c(42) : error C2065: 'prev' : undeclared identifier C:\Users\Administrator\Desktop\数据结构实验报告\test\实验2.c(43) : error C2143: syntax error : missing ';' before 'type' C:\Users\Administrator\Desktop\数据结构实验报告\test\实验2.c(43) : error C2143: syntax error : missing ';' before 'type' C:\Users\Administrator\Desktop\数据结构实验报告\test\实验2.c(43) : error C2143: syntax error : missing ')' before 'type' C:\Users\Administrator\Desktop\数据结构实验报告\test\实验2.c(43) : error C2143: syntax error : missing ';' before 'type' C:\Users\Administrator\Desktop\数据结构实验报告\test\实验2.c(43) : error C2065: 'j' : undeclared identifier C:\Users\Administrator\Desktop\数据结构实验报告\test\实验2.c(43) : warning C4047: '!=' : 'int ' differs in levels of indirection from 'void *' C:\Users\Administrator\Desktop\数据结构实验报告\test\实验2.c(43) : error C2059: syntax error : ')' C:\Users\Administrator\Desktop\数据结构实验报告\test\实验2.c(43) : error C2143: syntax error : missing ';' before '{' C:\Users\Administrator\Desktop\数据结构实验报告\test\实验2.c(44) : error C2223: left of '->next' must point to struct/union C:\Users\Administrator\Desktop\数据结构实验报告\test\实验2.c(47) : warning C4047: '==' : 'int ' differs in levels of indirection from 'void *' C:\Users\Administrator\Desktop\数据结构实验报告\test\实验2.c(49) : error C2223: left of '->next' must point to struct/union C:\Users\Administrator\Desktop\数据结构实验报告\test\实验2.c(49) : error C2223: left of '->next' must point to struct/union C:\Users\Administrator\Desktop\数据结构实验报告\test\实验2.c(50) : error C2223: left of '->next' must point to struct/union C:\Users\Administrator\Desktop\数据结构实验报告\test\实验2.c(58) : error C2275: 'Node' : illegal use of this type as an expression C:\Users\Administrator\Desktop\数据结构实验报告\test\实验2.c(8) : see declaration of 'Node' C:\Users\Administrator\Desktop\数据结构实验报告\test\实验2.c(58) : error C2065: 'temp' : undeclared identifier C:\Users\Administrator\Desktop\数据结构实验报告\test\实验2.c(60) : warning C4047: '=' : 'int ' differs in levels of indirection from 'struct Node *' C:\Users\Administrator\Desktop\数据结构实验报告\test\实验2.c(62) : warning C4022: 'free' : pointer mismatch for actual parameter 1 C:\Users\Administrator\Desktop\数据结构实验报告\test\实验2.c(66) : error C2275: 'Node' : illegal use of this type as an expression C:\Users\Administrator\Desktop\数据结构实验报告\test\实验2.c(8) : see declaration of 'Node' C:\Users\Administrator\Desktop\数据结构实验报告\test\实验2.c(67) : error C2143: syntax error : missing ';' before 'type' C:\Users\Administrator\Desktop\数据结构实验报告\test\实验2.c(67) : error C2143: syntax error : missing ';' before 'type' C:\Users\Administrator\Desktop\数据结构实验报告\test\实验2.c(67) : error C2143: syntax error : missing ')' before 'type' C:\Users\Administrator\Desktop\数据结构实验报告\test\实验2.c(67) : error C2143: syntax error : missing ';' before 'type' C:\Users\Administrator\Desktop\数据结构实验报告\test\实验2.c(67) : error C2223: left of '->next' must point to struct/union C:\Users\Administrator\Desktop\数据结构实验报告\test\实验2.c(67) : error C2059: syntax error : ')' C:\Users\Administrator\Desktop\数据结构实验报告\test\实验2.c(67) : error C2143: syntax error : missing ';' before '{' C:\Users\Administrator\Desktop\数据结构实验报告\test\实验2.c(68) : error C2223: left of '->next' must point to struct/union C:\Users\Administrator\Desktop\数据结构实验报告\test\实验2.c(71) : error C2223: left of '->next' must point to struct/union C:\Users\Administrator\Desktop\数据结构实验报告\test\实验2.c(73) : error C2223: left of '->next' must point to struct/union C:\Users\Administrator\Desktop\数据结构实验报告\test\实验2.c(74) : error C2223: left of '->next' must point to struct/union C:\Users\Administrator\Desktop\数据结构实验报告\test\实验2.c(74) : error C2223: left of '->next' must point to struct/union C:\Users\Administrator\Desktop\数据结构实验报告\test\实验2.c(75) : warning C4022: 'free' : pointer mismatch for actual parameter 1 Error executing cl.exe. test.exe - 32 error(s), 6 warning(s)
最新发布
10-24
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值