我的c++程序编译时不能打开头文件#include<string.h>,是什么原因啊?

来源:百度知道 编辑:UC知道 时间:2024/06/27 09:56:11
#include<STRING.H>
#include<iostream.h>
void main()
{string st("the expense of spirit\n");
cout<<"the size of"<<st<<"is"<<st.size()<<"charecters,including the newline\n";
}
编译不能通过,编译器不能识别string是一个已定义的类,给出的错误是error C2065: 'string' : undeclared identifier
我随后添加了#include <atlbase.h>,还是不行。代码有错误吗?

#include <string.h> 和#include<string> 不同
前者是字符串函数的头文件,包含的是字符串处理函数。
而后者是c++里定义的字符串类,比前者的功能更强大。
字符串的拼接和赋值可以用+和=(操作符重载)等等。
在#include<string> 后下一行要加using namespace std; 或者 using std::string;(因为这个类是定义在名空间里的) 后就可以用了。

为什么不用

#include <cstring>

当出现错误:error C2065: ''CComVariant'' : undeclared identifier时
包含头文件:#include <atlbase.h> 即可

把前两行这么改:
#include<string>
#include<iostream>
using namespace std;

1.#include <string.h>
#include<iostream>

using namespace std;
2."<<st 不对,st是对象