簡體   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