流的std::无法将std::string写入文件
std::ofstream not able to write std::string to file
在发布之前的最后一次检查时已经解决了这个问题,但调试起来有点不好(至少对新手来说是这样),所以我无论如何都会发布它-可以随意删除。
问题是,在下面的标记行中,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;// << ",";
^
因此,解决方案:缺少标头。
#include <iostream>
已经在那里了,但没有
#include <fstream>
相关文章:
- 多个文件的内存分配错误"在抛出 'std :: bad_alloc' what (): std :: bad_alloc 的实例后终止调用" [C++]
- 如何传递多个 std::文件系统选项?
- 遍历顺序由 std::文件系统directory_iterator给出
- 使 std::fstream 写入文件末尾,但从头开始读取
- 是否可以使用 Python csv 阅读器读取使用 C++ std::setw 生成的文件?
- std::ifstream 在读取文件中最后一项时设置 eofbit,但仅在读取数值类型时发生
- C++:std::ofstream 方法 open() 在第二次迭代时擦除打开的 ifstream 文件
- 'std::wstring_convert'尽可能多地转换(从 UTF8 文件读取块)
- 是否可以使用 std::文件系统获取根名称列表?
- llvm 9在10.15之前的mac版本上支持std::文件系统吗
- std::文件系统根路径如何将自身作为父路径?
- 将 std::文件系统头文件添加到我的程序时出现编译错误
- 使用 std::文件系统替换文件
- std::文件系统和 std::experimental::文件系统问题在不同的编译器中
- XCode链接到STD ::文件系统的哪个库
- 使用 std::文件系统输出作为 LPCWSTR
- Visual Studio 中的 std 文件中的错误
- std文件流的相对路径
- 如何返回STD文件流
- 为什么在 C++1y 中没有接口来获取 std 文件系统库扩展中的用户/组信息