为什么会出现这个编译器错误?- 调用'std::basic_ofstream<char>::open(std::string&)'没有匹配功能
why this compiler error? - no matching function for call to 'std::basic_ofstream<char>::open(std::string&)'
这适用于Visual Studio,也适用于一台计算机上的GCC 4.9.2。
但在另一台计算机上,我认为它是相同的GCC 4.9.2编译器,但它给了我这个错误。
我是不是错过了什么?怎么回事?
#include <iostream>
#include <fstream>
#include <string>
using namespace std;
int main()
{
string filename;
filename = "teststring";
ofstream fout;
fout.open(filename);
fout << "Hello world!" << endl;
fout.close();
return 0;
}
||=== Build: Debug in fileiotest (compiler: TDM32 GNU GCC Compiler 4.9.2 dw2) ===|
F:UsersXXXcppfileiotestmain.cpp||In function 'int main()':|
F:UsersXXXcppfileiotestmain.cpp|12|error: no matching function for call to 'std::basic_ofstream<char>::open(std::string&)'|
F:UsersXXXcppfileiotestmain.cpp|12|note: candidate is:|
F:TDM-GCC-32libgccmingw324.9.2-dw2includec++fstream|716|note: void std::basic_ofstream<_CharT, _Traits>::open(const char*, std::ios_base::openmode) [with _CharT = char; _Traits = std::char_traits<char>; std::ios_base::openmode = std::_Ios_Openmode]|
F:TDM-GCC-32libgccmingw324.9.2-dw2includec++fstream|716|note: no known conversion for argument 1 from 'std::string {aka std::basic_string<char>}' to 'const char*'|
||=== Build failed: 1 error(s), 0 warning(s) (0 minute(s), 0 second(s)) ===|
这个重载在C++11中是新的,这意味着您需要在构建命令中传递-std=c++11
。
在C++03中,我们曾经这样写:
fout.open(filename.c_str());
相关文章:
- 使用std::multimap迭代器创建std::list
- C++中std::resize(n)和std::shrink_to_fit之间的区别
- 来自 std::list 的迭代器 .end() 按预期返回"0xcdcdcdcdcdcdcdcd"但 .begin()
- C++17复制构造函数,在std::unordereded_map上进行深度复制
- 如何导出包含具有"std::unique_ptr"值的"std::map"属性的
- 从持续时间构造std::chrono::system_clock::time_point
- std::具有相同基类的类的变体
- std::向量与传递值的动态数组
- 使用std::vector的OpenCL矩阵乘法
- std::map<struct,struct>::find 找不到匹配项,但是如果我循环通过 begin() 到 end(),我在那里看到匹配项
- std::condition_variable::wait()如何评估给定的谓词
- 如何获取std::result_of函数的返回类型
- std::原子加载和存储都需要吗
- 将对象移动到std::shared_ptr
- std::vector<;uint8_t>;当C++11/14启用时,手动复制而不是调用memcpy
- 是std :: set&lt; std :: future&gt;不可能存在
- 在修改列表后,std :: list&lt; t&gt; :: end()的值是否会更改
- 使用 std::vector<boost::shared_ptr<Base_Class>> 或 boost::p tr_vector 的性能注意事项是什么<Base>
- std::map<std::set, double> AND std:<long>map< std::p air<long, long>, double>
- 如何获取std::vector<DMatch>