无法在 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/