繁体   English   中英

与&#39;operator &lt;&lt;&#39;不匹配(操作数类型为&#39;std :: fstream {aka std :: basic_fstream <char> }”和“单词”)

[英]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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM