std::ofstream 无法将 std::string 写入文件

std::ofstream not able to write std::string to file

在 post 之前最后一次检查时已经解决了这个问题,但是调试起来有点麻烦(至少对于新手来说),所以我还是 post 它 -随意删除。

问题是在下面标记的行中,ofstream 似乎不能写一个简单的字符串;模板似乎是问题所在:

template <typename T>
void appendVectorToCSV(const std::string& header, std::vector<T> row,
        const std::string& outfilename){
    std::ofstream fout(outfilename);
    fout << header;// << ",";    /* The error line 80 */
    ...

这给出了错误:

varUtils.hpp: In function ‘void appendVectorToCSV(std::string&, const std::vector<_RealType>&, const string&)’:
varUtils.hpp:80:10: error: no match for ‘operator<<’ (operand types are ‘std::ofstream {aka std::basic_ofstream<char>}’ and ‘std::string {aka std::basic_string<char>}’)
     fout << header;// << ",";
          ^
varUtils.hpp:80:10: note: candidates are:
...
/usr/include/c++/4.8/complex:524:5: note: template<class _Tp, class _CharT, class _Traits> std::basic_ostream<_CharT, _Traits>& std::operator<<(std::basic_ostream<_CharT, _Traits>&, const std::complex<_Tp>&)
     operator<<(basic_ostream<_CharT, _Traits>& __os, const complex<_Tp>& __x)
     ^
/usr/include/c++/4.8/complex:524:5: note:   template argument deduction/substitution failed:
...
varUtils.hpp:80:13: note:   ‘std::ofstream {aka std::basic_ofstream<char>}’ is not derived from ‘std::basic_ostream<_CharT, _Traits>’
     fout << header;// << ",";
             ^

所以,解决方案: 一个失踪header。

#include <iostream> 

已经存在,但没有

#include <fstream>