如何在VC++6.0上解决这个问题

How can i solve this on VC++ 6.0

本文关键字:解决 问题 VC++6      更新时间:2023-10-16

我有这个简单的代码,但我不知道这个的错误在哪里

#include <iostream.h>
#include <fstream.h>
#include <string.h>
using namespace std;
int main(){
    string s;
    cout<<"Entrer nom de fichier avec le source";
    cin s;
    ifstream fout;
    fout.open(s);
    s=fout.getche();
    fout.close();
    cout<<s;
    return 0;
}   

导致编译后显示的错误是:

d:workespace3.cpp(5) : error C2871: 'std' : does not exist or is not a namespace
d:workespace3.cpp(8) : error C2653: 'std' : is not a class or namespace name
d:workespace3.cpp(8) : error C2065: 'string' : undeclared identifier
d:workespace3.cpp(8) : error C2146: syntax error : missing ';' before identifier 's'
d:workespace3.cpp(8) : error C2065: 's' : undeclared identifier
d:workespace3.cpp(10) : error C2146: syntax error : missing ';' before identifier 's'
d:workespace3.cpp(13) : error C2039: 'getche' : is not a member of 'ifstream'
c:program files (x86)microsoft visual studiovc98includefstream.h(98) : see declaration of 'ifstream'
Error executing cl.exe.
workespace3.obj - 7 error(s), 0 warning(s)

不要使用include文件的.h形式,这些形式是为了与C向后兼容。例如使用#include <string>

您有许多错误:

1-您正在使用不推荐使用的头文件。标准C++库头文件的头文件中没有".h"。因此,这将是:

#include <iostream>
#include <fstream>
#include <string>

2-getche()不是一个合适的ifstream方法。以下是ifstream的完整方法列表:

http://www.cplusplus.com/reference/fstream/ifstream/

您可能打算使用get()或getline()

3-您在"cin"answers"s"之间缺少"­­>>"。

4-您使用的是一个非常旧的IDE。有许多更新的免费IDE。值得注意的是,您可以免费获得VC++2012 Express。它将更加符合标准,还包括更好的工具和对C++11 的支持

如果编译器支持C++98标准标头,则使用它们

#include <iostream> // no .h

如果它只支持您所包含的古老的ISO之前的标头,那么就不提namespace std了。在那些黑暗的日子里,标准库只是被转储到全局命名空间中。

我会考虑使用本世纪的编译器;你会发现从那些对20世纪90年代记忆不太完美的人那里得到帮助更容易。