gpt4 book ai didi

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

转载 作者:塔克拉玛干 更新时间:2023-11-03 00:05:18 26 4
gpt4 key购买 nike

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

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com