各位C++大虾们,我的String库不能用怎么解决?

来源:百度知道 编辑:UC知道 时间:2024/06/23 19:19:02
请看下面程序及出错信息:

#include "stdafx.h"
#include "iostream.h"
#include "fstream.h"
#include "ctype.h"
#include "string.h"
#include "stdlib.h"

int main(int argc, char* argv[])
{
string temp;
ifstream fin;
ofstream fout;

fin.open("test.txt");
if(fin.fail())
{
cout << "Input file opening failed.\n";
exit(1);
}

fin >> temp;
cout << temp.substr(0,1023);

return 0;
}

D:\wsy2121\VisualC++Pro\ReadFile\ReadFile.cpp(155) : error C2065: 'string' : undeclared identifier
D:\wsy2121\VisualC++Pro\ReadFile\ReadFile.cpp(155) : error C2146: syntax error : missing ';' before identifier 'temp'
D:\wsy2121\VisualC++Pro\ReadFile\ReadFile.cpp(155) : error C2065: 'temp&

呃,VC6支持名字空间的,实在不行,打个SP6补丁试试。

------------------------------------
#include "string.h"
你包含的是标准C的string.h文件,而不是C++里面的STL:string头文件,将上面的include修改为:
#include "stdafx.h"
#include "iostream"
#include "fstream"
#include "cctype"
#include "string" // 你上面的问题,修改这个就可以了。
#include "cstdlib"

using namespace std; // 引入std全局名字空间

换一个编译器,
试试devc++
小巧好用!