Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/visual-studio/8.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 带有g+的ifstream错误+;但是使用VisualStudio编译_C++_Visual Studio_Compiler Errors_G++ - Fatal编程技术网

C++ 带有g+的ifstream错误+;但是使用VisualStudio编译

C++ 带有g+的ifstream错误+;但是使用VisualStudio编译,c++,visual-studio,compiler-errors,g++,C++,Visual Studio,Compiler Errors,G++,我一直在使用VisualStudio进行我正在进行的一个项目,尽管它也必须在Linux上使用GCC进行编译。我已经完成了我的项目,它运行良好,但我将文件发送到我的Linux shell,收到一个错误,其中包含一行微不足道的代码: std::ifstream input(s); 这给了我一个错误,说没有匹配函数s是一个std::string。 尽管我正在查看ifstream的文档,但有人能告诉我为什么它在visualstudio下运行,而不是在GCC下运行吗?也许是GCC的旧版本 编辑:GCC版

我一直在使用VisualStudio进行我正在进行的一个项目,尽管它也必须在Linux上使用GCC进行编译。我已经完成了我的项目,它运行良好,但我将文件发送到我的Linux shell,收到一个错误,其中包含一行微不足道的代码:

std::ifstream input(s);
这给了我一个错误,说没有匹配函数<顺便说一下,code>s是一个
std::string
。 尽管我正在查看ifstream的文档,但有人能告诉我为什么它在visualstudio下运行,而不是在GCC下运行吗?也许是GCC的旧版本

编辑:GCC版本为4.2.1,确切错误为:

error: no matching function for call to 'std::basic_ifstream<char,  
std::char_traits<char>>::basic_ifstream(std::string&)'
错误:调用“std::basic\u ifstream::basic\u ifstream(std::string&)”时没有匹配的函数
编辑2:相关代码:

std::string s = "";
if(argc == 2)
    s = argv[1];
else{
    std::cout << "Bad filename?" << std::endl;
    return 1;
}
std::ifstream input(s);
std::string s=“”;
如果(argc==2)
s=argv[1];
否则{

std::cout下载最新版本的GCC,并使用
-std=c++0x
选项编译您的程序。在c++11中,流类具有以
std::string
为参数的构造函数,GCC默认情况下不启用c++11,因此需要通过提供
-std=c++0x
编译器选项来启用

如果无法使用C++11,请执行以下操作:

std::ifstream input(s.c_str());

这应该可以在C++03和C++11中编译。

这:很好;您可能应该发布完整的代码。@RoryWorker更新了我认为唯一相关的代码。我试图通过调用
open
来更改代码,但它也会出错。@AustinHenley:查看我更新的答案。另外,当您看到错误时,请发布它。请不要不要让我们猜测错误。
c_str
非常有效。谢谢。)