linux ubuntu 下 ‘string’ was not declared in this scope 报错 解析
报错如下:
-
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!遇到问题我首先来一个个的排除问题
error.h
-
#ifndef _ERROR_1_
-
#define _ERROR_1_
-
#include<cstring>
-
-
void cout_1(string a1);
-
-
#endif
error.c
-
#include<error.h>
-
#include<iostream>
-
using namespace std;
-
void cout_1(string str)
-
{
-
cout << str<< endl;
-
}
-
-
int main()
-
{
-
-
cout_1("test");
-
return 0;
-
-
}
运行效果:
-
aries@ubuntu:~/aries/Makefile/Demo$ g++ -o error error.cpp
-
aries@ubuntu:~/aries/Makefile/Demo$ ./error
-
test
2 把 main单独提出来看又会怎么样呢?
error_h.cpp
-
#include<error.h>
-
-
void cout_1(string str)
-
{
-
cout << str<< endl;
-
}
修改error.c
-
#include"error.h"
-
#include"error_h.cpp"
-
#include<iostream>
-
using namespace std;
-
-
int main()
-
{
-
-
cout_1("test");
-
return 0;
-
-
}
运行效果如下:
-
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时,导致了依赖关系错误。比如,头文件形成了一个环形依赖,
-
-
#ifndef FILE_A_
-
#define FILE_A_
-
#include <file b>
-
#endif
-
-
-
#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
-
#include<error.h>
-
#include<cstring>
-
#include<iostream>
-
using namespace std;
-
-
void cout_1(string str)
-
{
-
cout << str<< endl;
-
}
运行效果如下:
-
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
-
#ifndef _ERROR_1_
-
#define _ERROR_1_
-
#include<string>
-
-
void cout_1( std::string a1);
-
-
#endif
-
aries@ubuntu:~/aries/Makefile/Demo$
error_h.cpp
-
#include<error.h>
-
#include<string>
-
#include<iostream>
-
using namespace std;
-
-
void cout_1( std ::string str)
-
{
-
cout << str<< endl;
-
}
main.c
-
#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 的还是很不错。
还得感谢我的老同学帮忙:
-
#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前加上他的::了 你的博客写的不完美
linux ubuntu 下 ‘string’ was not declared in this scope 解析
原文:http://blog.csdn.net/u010236550/article/details/19901087