我收到以下编译错误:no match for 'operator<<' (operand types are 'std::fstream {aka std::basic_fstream<char>}' and 'Word')
此错误的原因是什么?
以下是重现错误的最小示例:
#include <fstream>
#include <cstring>
struct Word
{
char word[10];
char mean[20];
};
Word word;
void writeDataToFile()
{
std::fstream fileOutput("data.txt", std::ios::out | std::ios::binary);
// error handling left out for simplicity
fileOutput << word << std::endl;
}
int main()
{
strcpy(word.word, "Apple");
strcpy(word.mean, "Trai tao");
writeDataToFile();
return 0;
}
答案 0 :(得分:2)
您需要重载struct Word
的输出运算符,因为您在行fileOutput << a << endl;
上使用它。在output overloading on tutorialspoint和operator overloading on cppreference上查看这两个链接。