linux ubuntu 下 ‘string’ was not declared in this scope 解析

linux ubuntu 下 ‘string’ was not declared in this scope 报错 解析


报错如下:
  1. test2.h:5:17: error: ‘string’ is not a member of ‘std’  
  2. aries@ubuntu:~/aries/Makefile/Demo$ g++ -o main main.cpp   
  3. In file included from main.cpp:1:0:  
  4. test1.h:5:17: error: variable or field ‘test1_cout’ declared void  
  5. test1.h:5:17: error: ‘string’ was not declared in this scope  
  6. In file included from main.cpp:2:0:  
  7. test2.h:5:17: error: variable or field ‘test2_cout’ declared void  
  8. test2.h:5:17: error: ‘string’ was not declared in this scope  
  9. aries@ubuntu:~/aries/Makefile/Demo$   

ok!遇到问题我首先来一个个的排除问题



error.h
  1. #ifndef _ERROR_1_  
  2. #define _ERROR_1_  
  3. #include<cstring>  
  4.   
  5. void cout_1(string a1);  
  6.   
  7. #endif  
error.c
  1. #include<error.h>  
  2. #include<iostream>  
  3. using namespace std;  
  4. void cout_1(string str)  
  5. {  
  6.         cout << str<< endl;  
  7. }  
  8.   
  9. int main()  
  10. {  
  11.   
  12.         cout_1("test");  
  13.         return 0;  
  14.   
  15. }  

运行效果:
  1. aries@ubuntu:~/aries/Makefile/Demo$ g++ -o error error.cpp   
  2. aries@ubuntu:~/aries/Makefile/Demo$ ./error   
  3. test  

2 把 main单独提出来看又会怎么样呢?
error_h.cpp

  1. #include<error.h>  
  2.   
  3. void cout_1(string str)  
  4. {  
  5.         cout << str<< endl;  
  6. }  

修改error.c
  1. #include"error.h"  
  2. #include"error_h.cpp"  
  3. #include<iostream>  
  4. using namespace std;  
  5.   
  6. int main()  
  7. {  
  8.   
  9.     cout_1("test");  
  10.     return 0;  
  11.       
  12. }  

运行效果如下:
  1. aries@ubuntu:~/aries/Makefile/Demo$ g++ -o error error.cpp   
  2. In file included from error.cpp:1:0:  
  3. error.h:5:13: error: variable or field ‘cout_1’ declared void  
  4. error.h:5:13: error: ‘string’ was not declared in this scope  
  5. In file included from error.cpp:2:0:  
  6. error_h.cpp:3:13: error: variable or field ‘cout_1’ declared void  
  7. error_h.cpp:3:13: error: ‘string’ was not declared in this scope  
  8. error.cpp: In function ‘int main()’:  
  9. error.cpp:9:15: error: ‘cout_1’ was not declared in this scope  

OK !又出现上面的问题了。

疑问:为什么c++ 不可以 像c这样 呢?

3 我尝试了 google


然后上网找答案。在下面的几种情况下,会出现这种错误。

1.变量、函数、或者类未声明或者定义。这是最简单的情况。

2.头文件相互#include时,导致了依赖关系错误。比如,头文件形成了一个环形依赖,
 
  1. /***file a ****/  
  2.  #ifndef FILE_A_  
  3.  #define FILE_A_  
  4.  #include <file b>  
  5.  #endif   
  6.   
  7.  /****file b ***/  
  8.   #ifndef FILE_B_  
  9.  #define FILE_B_  
  10.  #include <file a>  
  11.  #endif   



  如果在file b中用到了file a中的变量、函数、类,那么由于#ifndef和#define的作用,file b中的#include <file a>语句将失去效果。

3.我遇到的情况:我在给头文件起名字的时候不小心和某个库中的头文件重名了,而在程序中又用来了这个库的这个头文件。这样, #ifndef XXXX 和 #ifndef XXXX中的宏重名了,我自己写的头文件就失去了效果。别的文件自然找不到这个头文件中的声明,就提示 was not decleared in this scope了。


仔细看了看,没有我想要的。

4 继续修改 
error_h.cpp

  1. #include<error.h>  
  2. #include<cstring>  
  3. #include<iostream>  
  4. using namespace std;  
  5.   
  6. void cout_1(string str)  
  7. {  
  8.         cout << str<< endl;  
  9. }  

运行效果如下:
  1. aries@ubuntu:~/aries/Makefile/Demo$ g++ -o error error.cpp   
  2. In file included from error.cpp:1:0:  
  3. error.h:5:13: error: variable or field ‘cout_1’ declared void  
  4. error.h:5:13: error: ‘string’ was not declared in this scope  

还好错误减少了! 不过还是error.h 报错。

我现在不知道如何修改了,求助!


--------------------------------------ok 已解决-----
代码如下:
error.h

  1. #ifndef _ERROR_1_  
  2. #define _ERROR_1_  
  3. #include<string>  
  4.   
  5. void cout_1( std::string a1);  
  6.   
  7. #endif  
  8. aries@ubuntu:~/aries/Makefile/Demo$   

error_h.cpp
  1. #include<error.h>  
  2. #include<string>  
  3. #include<iostream>  
  4. using namespace std;  
  5.   
  6. void cout_1( std ::string str)  
  7. {  
  8.         cout << str<< endl;  
  9. }  

main.c
  1. #include"error.h"  
  2. #include"error_h.cpp"  
  3. #include<iostream>  
  4. using namespace std;  
  5.   
  6. int main()  
  7. {  
  8.     std :: string a = "test";  
  9.     cout_1(a);  
  10.     return 0;  
  11.       
  12. }  

运行效果:



ok! 结束 

总结:我的原因是把  Cstring 是微软mfc的 搞成C++的头文件了
           不过前面google 的还是很不错。

还得感谢我的老同学帮忙:
  1. #include"error.h"    
  2.   
  3. #include"error_h.cpp"    
  4.   
  5. #include<iostream>    
  6.   
  7. using namespace std;    
  8.   
  9.     
  10.   
  11. int main()    
  12.   
  13. {    
  14.   
  15.     std :: string a = "test";    
  16.   
  17.     cout_1(a);    
  18.   
  19.     return 0;    
  20.   
  21.         
  22.   
  23. }   
  24. 有了using namespace std;就不用在string前加上他的::了 你的博客写的不完美  
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值