缺少类型说明符 - 假定为 int。注意: C++ 不支持默认 int---->解决方法

36 篇文章 0 订阅

写了一个对应让其出的错误(其实也挺不容易的喔 )

错误如下:

1>d:\work\win32project\testeachotherclude\testeachotherclude\test2.h(9): error C2143: 语法错误 : 缺少“;”(在“*”的前面)
1>d:\work\win32project\testeachotherclude\testeachotherclude\test2.h(9): error C4430: 缺少类型说明符 - 假定为 int。注意: C++ 不支持默认 int
1>d:\work\win32project\testeachotherclude\testeachotherclude\test2.h(9): error C4430: 缺少类型说明符 - 假定为 int。注意: C++ 不支持默认 int
1>d:\work\win32project\testeachotherclude\testeachotherclude\test2.h(10): error C2061: 语法错误: 标识符“Test1”
1>d:\work\win32project\testeachotherclude\testeachotherclude\testeachotherclude.cpp(15): error C2660: “Test2::className”: 函数不接受 1 个参数
1>d:\work\win32project\testeachotherclude\testeachotherclude\testeachotherclude.cpp(16): error C2039: “test1”: 不是“Test2”的成员
1>          d:\work\win32project\testeachotherclude\testeachotherclude\test2.h(7) : 参见“Test2”的声明
1>d:\work\win32project\testeachotherclude\testeachotherclude\testeachotherclude.cpp(16): error C2227: “->refMe”的左边必须指向类/结构/联合/泛型类型

上传Test方便大家看:

main.cpp

  1. #include "stdafx.h"  
  2. #include "Test1.h"  
  3. #include "Test2.h"  
  4.   
  5. int _tmain(int argc, _TCHAR* argv[])  
  6. {  
  7.     Test1* t1=new Test1();  
  8.     Test2* t2=new Test2();  
  9.     t1->className(t2);  
  10.     t1->test2->refMe();  
  11.   
  12.     t2->className(t1);  
  13.     t2->test1->refMe();  
  14.     return 0;  
  15. }  

再来看Test1的

Test1.h

  1. #ifndef Test1_H  
  2. #define Test1_H  
  3.   
  4. #include "Test2.h"  
  5.   
  6. class Test1  
  7. {  
  8. public:  
  9.     Test2* test2;  
  10.     void Test1::className(Test2* test2);  
  11.     void refMe();  
  12. };  
  13. #endif;  

Test1.cpp

  1. #include "Test2.h"  
  2.   
  3. void Test1::className(Test2* test2)  
  4. {  
  5.     this->test2=test2;  
  6.     std::cout<<"Test1  ";  
  7. }  
  8.   
  9. void Test1::refMe()  
  10. {  
  11.     std::cout<<"refMe Test1"<<std::endl;  
  12. }  

Test2.h

  1. #ifndef Test2_H  
  2. #define Test2_H  
  3.   
  4. #include "Test1.h"  
  5.   
  6. class Test2  
  7. {  
  8. public:  
  9.     Test1* test1;  
  10.     void className(Test1* test1);  
  11.     void refMe();  
  12. };  
  13. #endif  

Test2.cpp

  1. #include "StdAfx.h"  
  2. #include "Test2.h"  
  3. #include <iostream>  
  4.   
  5. void Test2::className(Test1* test1)  
  6. {  
  7.     this->test1=test1;  
  8.     std::cout<<"Test2  ";  
  9. }  
  10.   
  11. void Test2::refMe()  
  12. {  
  13.     std::cout<<"refMe Test2"<<std::endl;  
  14. }  


然后运行就报上面的一对错误了...

仔细看看发现两个头文件都彼此包含了,就是这个问题啦...那我们怎么解决了,上传我的代码,直接看这样更容易理解  main.cpp不变

Test1.h

  1. #ifndef Test1_H  
  2. #define Test1_H  
  3. //#include "Test2.h" //用下面的方式替代  
  4. class Test2;//注意:只是告诉编译器,需要这个类,其他功能结构等都没  
  5.   
  6. class Test1  
  7. {  
  8. public:  
  9.     Test2* test2;  
  10.     void Test1::className(Test2* test2);  
  11.     void refMe();  
  12. };  
  13. #endif;  


 Test1.cpp

  1. #include "StdAfx.h"  
  2. #include "Test1.h"  
  3. #include <iostream>  
  4.   
  5. #include "Test2.h" //注意:这里才是真正的包含  
  6.   
  7. void Test1::className(Test2* test2)  
  8. {  
  9.     this->test2=test2;  
  10.     std::cout<<"Test1  ";  
  11. }  
  12.   
  13. void Test1::refMe()  
  14. {  
  15.     std::cout<<"refMe Test1"<<std::endl;  
  16. }  


Tes2的我就不贴了,一样的道理,编译运行ok ....

  • 4
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 4
    评论
### 回答1: 这个错误提示意味着你在声明变量时没有指定变量的类型,而 C++支持默认类型int。因此,编译器无法确定变量的类型,从而导致了错误。 要解决这个问题,你需要在声明变量时显式地指定变量的类型。例如,如果你想声明一个整数变量,应该这样写: ``` int myInt; ``` 如果你想声明一个浮点数变量,应该这样写: ``` float myFloat; ``` 如果你已经指定了变量的类型,但仍然收到此错误提示,那么可能是因为你的代码中存在语法错误或拼写错误。你可以检查一下代码中是否有这样的问题。 ### 回答2: error c4430是指在C语言编译器中出现了缺少类型说明符的错误,提示假定int类型。需要注意的是,C语言不支持默认int类型,因此如果在代码中没有显式地声明变量类型,编译器会报出这个错误。 这个错误通常出现在以下两种情况下: 1. 变量没有声明类型 在C语言中,定义一个变量必须先声明它的类型。如果一个变量没有声明类型,编译器就无法识别它的类型,在编译时就会出现error c4430错误。 例如: x = 10; 这段代码就会出现error c4430错误,因为x没有声明类型。 要解决这个错误,我们需要在定义变量时同时声明它的类型,例如: int x = 10; 2. 头文件顺序问题 另一种情况是,在头文件中定义变量时,如果头文件的顺序不正确,就会导致编译器无法找到变量的定义,从而产生error c4430错误。 例如: #include "b.h" #include "a.h" 这种情况下,如果变量在b.h中定义而在a.h中使用,就会出现error c4430错误。 要解决这个问题,我们需要将头文件包含的顺序调整正确,将使用的头文件放在前面,定义的头文件放在后面,如下所示: #include "a.h" #include "b.h" 总之,出现error c4430错误时,需要仔细检查代码中的变量声明和头文件的顺序,保证变量都被正确声明并且头文件顺序正确。 ### 回答3: error c4430: 缺少类型说明符 - 假定int。这个错误提示是在C++编译过程中出现的,它表明在代码中缺少类型说明符,导致编译器无法识别其类型,因而会默认int类型。所以我们需要在代码中添加类型说明符,以便编译器正确识别。 需要注意的是,C语言并不支持默认int类型,这是与C++语言不同的地方。在C中,如果我们没有显式地指定变量的类型,编译器会默认为未定义类型,这将导致编译错误。而在C++中,如果我们没有显式指定变量类型,编译器会默认int类型,这就是为什么我们会看到这个错误提示。 为了解决这个错误,我们应该检查代码中是否有缺少类型说明符的情况,并为这些变量添加适当的类型说明符。如果代码中使用了大量的变量而没有指定类型,可以考虑在头文件中定义一个结构体,将这些变量作为结构体的成员,这样可以统一管理,并且可以避免出现类型错误。 在C++中,我们可以使用auto关键字来实现自动类型推断,这样可以避免一些类型错误,但需要注意的是,auto并不适用于所有情况,有些类型必须我们手动指定。 总之,解决error c4430错误,需要引起我们重视和审慎处理,要深入分析代码,增加类型说明符,并严格遵守C++语法规范,这样才能避免出现类似错误。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值