c++ - 无法在 gnu linux c++ 中写入 ostream 错误 : invalid operands of types ofstream and ‘const char [2]’ to binary ‘operator

标签 c++ linux c++11 fstream

无法在 Linux 上编译这个小代码:

#include <fstream>
#include <string>
#include <iostream>
#include <stdio.h>
using namespace std;
int main(){
    char fileName[512];
    sprintf(fileName, "test");
    ofstream iriFile(string(fileName));
    iriFile<<",";
    return 0;
}

我像这样编译:g++ test.cpp 并得到这个:

test.cpp:12:11: error: invalid operands of types ‘std::ofstream(std::__cxx11::string) {aka std::basic_ofstream(std::__cxx11::basic_string)}’ and ‘const char [2]’ to binary ‘operator<<’ iriFile<<",";

可能是什么原因?

最佳答案

好的,解决方案是删除隐式 string() 创建:

string sFileName(fileName)
ofstream iriFile(sFileName);

关于c++ - 无法在 gnu linux c++ 中写入 ostream 错误 : invalid operands of types ofstream and ‘const char [2]’ to binary ‘operator,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48172145/

相关文章:

linux - 如何找到文件系统并发问题?

linux - bash 脚本不能通过 crontab 工作

c++ - 部分填充模板作为模板template的参数

c++ - 静态类成员的初始化。为什么要构造函数?

c++ - 计算飞行距离的程序

c++ - 如何隐藏数独表中的数字?

c++ - 我在调试下面的代码时遇到问题。我在 visual studio 中得到正确的输出,

Linux/Raspberry Pi 命令帮助四舍五入到小数点后 2 位

c++ - 如何在 C++ 中只访问 unordered_set 的元素?

c++ - 优化 volatile 变量