C++ Error no matching function for call to 'std::basic_ofstream::basic_ofstream(std::string&)

1433 ワード

Table of Contents
に質問
説明:
解決方法:
に質問
string filename = "1.txt";  
ifstream fin;  
fin.open(filename);  

上記の文では、次のエラーが発生します.
error: no matching function for call to 'std::basic_ifstream::basic_ofstream(std::string&)
説明:std::ofstream  can only be constructed with a  std::string  if you have C++11 or higher. Typically that is done with  -std=c++11  (gcc, clang). If you do not have access to c++11 then you can use the  c_str()  function of  std::string  to pass a  const char *  to the  ofstream  constructor.
Also as Ben has pointed out you are using an empty string for the second parameter to the constructor. The second parameter if proivided needs to be of the type  ios_base::openmode .
With all this your code should be
ofstream entrada(asegurado); // C++11 or higher

or
ofstream entrada(asegurado.c_str());  // C++03 or below

原文住所:住所
解決方法:
つまり、私がここで使用しているC++コンパイルバージョンは比較的低いので、ここでは解決方法を使用することができます.c_str()メソッド.
string filename = "1.txt";  
ifstream fin;  
fin.open(filename.c_str());  

もちろん、2つ目の解決策もありますが、lowを比較すると:
cout<>fileName;
ofstream fout(fileName);

 
作者:无涯明月
下篇:C++sort関数