linux ubuntu 下 ‘string’ was not declared in this scope 解析
2014-02-25 10:12
453 查看
linux ubuntu 下 ‘string’ was not declared in this scope 报错 解析
报错如下:[cpp] view
plaincopyprint?
![](https://code.csdn.net/assets/CODE_ico.png)
test2.h:5:17: error: ‘string’ is not a member of ‘std’
aries@ubuntu:~/aries/Makefile/Demo$ g++ -o main main.cpp
In file included from main.cpp:1:0:
test1.h:5:17: error: variable or field ‘test1_cout’ declared void
test1.h:5:17: error: ‘string’ was not declared in this scope
In file included from main.cpp:2:0:
test2.h:5:17: error: variable or field ‘test2_cout’ declared void
test2.h:5:17: error: ‘string’ was not declared in this scope
aries@ubuntu:~/aries/Makefile/Demo$
ok!遇到问题我首先来一个个的排除问题
1
error.h
[cpp] viewplaincopyprint?
![](https://code.csdn.net/assets/CODE_ico.png)
#ifndef _ERROR_1_
#define _ERROR_1_
#include<cstring>
void cout_1(string a1);
#endif
error.c
[cpp] viewplaincopyprint?
![](https://code.csdn.net/assets/CODE_ico.png)
#include<error.h>
#include<iostream>
using namespace std;
void cout_1(string str)
{
cout << str<< endl;
}
int main()
{
cout_1("test");
return 0;
}
运行效果:
[cpp] view
plaincopyprint?
![](https://code.csdn.net/assets/CODE_ico.png)
aries@ubuntu:~/aries/Makefile/Demo$ g++ -o error error.cpp
aries@ubuntu:~/aries/Makefile/Demo$ ./error
test
2 把 main单独提出来看又会怎么样呢?
error_h.cpp
[cpp] viewplaincopyprint?
![](https://code.csdn.net/assets/CODE_ico.png)
#include<error.h>
void cout_1(string str)
{
cout << str<< endl;
}
修改error.c
[cpp] view
plaincopyprint?
![](https://code.csdn.net/assets/CODE_ico.png)
#include"error.h"
#include"error_h.cpp"
#include<iostream>
using namespace std;
int main()
{
cout_1("test");
return 0;
}
运行效果如下:
[cpp] view
plaincopyprint?
![](https://code.csdn.net/assets/CODE_ico.png)
aries@ubuntu:~/aries/Makefile/Demo$ g++ -o error error.cpp
In file included from error.cpp:1:0:
error.h:5:13: error: variable or field ‘cout_1’ declared void
error.h:5:13: error: ‘string’ was not declared in this scope
In file included from error.cpp:2:0:
error_h.cpp:3:13: error: variable or field ‘cout_1’ declared void
error_h.cpp:3:13: error: ‘string’ was not declared in this scope
error.cpp: In function ‘int main()’:
error.cpp:9:15: error: ‘cout_1’ was not declared in this scope
OK !又出现上面的问题了。
疑问:为什么c++ 不可以 像c这样 呢?
3 我尝试了 google
然后上网找答案。在下面的几种情况下,会出现这种错误。1.变量、函数、或者类未声明或者定义。这是最简单的情况。
2.头文件相互#include时,导致了依赖关系错误。比如,头文件形成了一个环形依赖,
[cpp] viewplaincopyprint?
![](https://code.csdn.net/assets/CODE_ico.png)
/***file a ****/
#ifndef FILE_A_
#define FILE_A_
#include <file b>
#endif
/****file b ***/
#ifndef FILE_B_
#define FILE_B_
#include <file a>
#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
[cpp] view
plaincopyprint?
![](https://code.csdn.net/assets/CODE_ico.png)
#include<error.h>
#include<cstring>
#include<iostream>
using namespace std;
void cout_1(string str)
{
cout << str<< endl;
}
运行效果如下:
[cpp] view
plaincopyprint?
![](https://code.csdn.net/assets/CODE_ico.png)
aries@ubuntu:~/aries/Makefile/Demo$ g++ -o error error.cpp
In file included from error.cpp:1:0:
error.h:5:13: error: variable or field ‘cout_1’ declared void
error.h:5:13: error: ‘string’ was not declared in this scope
还好错误减少了!不过还是error.h 报错。
我现在不知道如何修改了,求助!
--------------------------------------ok 已解决-----
代码如下:
error.h
[cpp] view
plaincopyprint?
![](https://code.csdn.net/assets/CODE_ico.png)
#ifndef _ERROR_1_
#define _ERROR_1_
#include<string>
void cout_1( std::string a1);
#endif
aries@ubuntu:~/aries/Makefile/Demo$
error_h.cpp
[cpp] view
plaincopyprint?
![](https://code.csdn.net/assets/CODE_ico.png)
#include<error.h>
#include<string>
#include<iostream>
using namespace std;
void cout_1( std ::string str)
{
cout << str<< endl;
}
main.c
[cpp] view
plaincopyprint?
![](https://code.csdn.net/assets/CODE_ico.png)
#include"error.h"
#include"error_h.cpp"
#include<iostream>
using namespace std;
int main()
{
std :: string a = "test";
cout_1(a);
return 0;
}
运行效果:
ok! 结束
总结:我的原因是把 Cstring 是微软mfc的 搞成C++的头文件了
不过前面google 的还是很不错。
还得感谢我的老同学帮忙:
[cpp] view
plaincopyprint?
![](https://code.csdn.net/assets/CODE_ico.png)
#include"error.h"
#include"error_h.cpp"
#include<iostream>
using namespace std;
int main()
{
std :: string a = "test";
cout_1(a);
return 0;
}
有了using namespace std;就不用在string前加上他的::了 你的博客写的不完美
相关文章推荐
- 经典问题---'cout' /string was not declared in this scope
- Codeblocks编译C++出现to_string is not a member of std 或者 to_string was not declared in this scope的解决方法
- error: ‘to_string’ was not declared in this scope
- C++出现to_string is not a member of std 或者 to_string was not declared in this scope的解决方法
- string was not declared in this scope问题解决方法
- 编译opencv 2.4.13error:1 /usr/include/string.h:652:42: error: ‘memcpy’ was not declared in this scope
- error: 'jstringToString' was not declared in this scope
- ruby 安装gem报错:`SSL_set_tlsext_host_name' was not declared in this scope
- common.h:178:47: error: 'UINT64_C' was not declared in this scope
- c++中[Error] 'cin' was not declared in this scope 是什么意思
- ffmpeg中遇到UINT64_C’ was not declared in this scope
- ‘close’ was not declared in this scope
- mingw32-make+cmake:error: 'nullptr' was not declared in this scope解决方法
- opencv 2.3交叉编译时出现dist.h:63:68: error: 'fabsl' was not declared in this scope
- 【STL源码剖析】--error: ‘hashtable’ was not declared in this scope
- 使用BOOST库 多线程ThreadPool 编译错误"TIME_UTC was not declared in this scope"
- Arduino ide 1.6.9 遇到的问题 error: 'TKD2' was not declared in this scope
- error: ‘errno’ was not declared in this scope
- 'wxCHANGE_DIR' was not declared in this scope
- g++ c++ error: 'malloc' was not declared in this scope