![](/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.