'operator }'和'Word')

时间:2023-01-31 16:13:05

I got this error: D:\Backup\TuDien\TestWrite.cpp [Error] no match for 'operator<<' (operand types are 'std::fstream {aka std::basic_fstream}' and 'Word')

我收到此错误:D:\ Backup \ TuDien \ TestWrite.cpp [Error]与'operator <<'不匹配(操作数类型为'std :: fstream {aka std :: basic_fstream}'和'Word')

Please help me to fix this problem. Thank you very much!

请帮我解决这个问题。非常感谢你!

Here is the code:

这是代码:

#include <fstream>
#include<stdio.h>
#include<string.h>
#include<ctype.h>
#include<conio.h>
#include<stdlib.h>
using namespace std;
struct Word
{
char word[10];
char mean[20];  
};
Word word;
void writeDataToFile()
{
std::fstream fileOutput("D:/data.txt", std::ios::out | std::ios::binary);


if (fileOutput.fail())
{
    std::cout << "Cannot open file at " << "D:/data.txt" << std::endl;
    return;
}

        Word a;
        strcpy(a.mean,word.mean);
        strcpy(a.word,word.word);
        fileOutput << a << endl;    
}

void readDataFromFile()
{
std::ifstream fileInput("D:/data.txt");

if (fileInput.fail())
{
    std::cout << "Cannot open file at " << "D:/data.txt" << std::endl;
    return;
}

while (!fileInput.eof())
{
    char line[255];
    fileInput.getline(line, 255);
    std::cout << line << std::endl;
}
}

int main()
{   
strcpy(word.word,"Apple");
strcpy(word.mean,"Trai tao");
writeDataToFile();
readDataFromFile();
return 0;
}

1 个解决方案

#1


0  

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.

您需要重载struct Word的输出运算符,因为您在行fileOutput << a << endl;上使用它。查看关于tutorialspoint上的输出重载和cppreference上的运算符重载的这两个链接。

#1


0  

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.

您需要重载struct Word的输出运算符,因为您在行fileOutput << a << endl;上使用它。查看关于tutorialspoint上的输出重载和cppreference上的运算符重载的这两个链接。