user7942531
user7942531

Reputation: 1

no match for 'operator<<' (operand types are 'std::fstream {aka std::basic_fstream<char>}' and 'Word')

I got the following compiler error: no match for 'operator<<' (operand types are 'std::fstream {aka std::basic_fstream<char>}' and 'Word')

What is the cause of this error?

Below is a minimal example to reproduce the errror:

#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;
}

Upvotes: 0

Views: 2882

Answers (1)

eozd
eozd

Reputation: 1203

You need to overload the output operator for struct Word since you are using it on line fileOutput << a << endl;. Check out these two links on output overloading on tutorialspoint and operator overloading on cppreference.

Upvotes: 2

Related Questions