![](/img/trans.png)
[英]error: conversion from 'const char [10]' to non-scalar type 'std::fstream {aka std::basic_fstream<char>}' requested
[英]no match for 'operator<<' (operand types are 'std::fstream {aka std::basic_fstream<char>}' and 'Word')
我收到以下编译器错误: 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;
}
您需要重载struct Word
的输出运算符,因为在fileOutput << a << endl;
行上使用了它fileOutput << a << endl;
。 请查看这两节有关tutorialspoint的输出重载和cppreference的运算符重载的链接。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.