Ich habe diesen Fehler: D: \ Backup \ TuDien \ TestWrite.cpp [ Fehler] keine Übereinstimmung für 'Operator < <' (Operandentypen sind 'std :: fstream {aka std :: basic_fstream}' und 'Word')keine Übereinstimmung für 'Operator <<' (Operandentypen sind 'std :: fstream {aka std :: basic_fstream <char>}' und 'Word')
Bitte helfen Sie mir, dieses Problem zu beheben. Vielen Dank! Hier
ist der 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;
}
Der Fehler ist ziemlich klar: Ihr 'fileOutput << a << endl; 'versucht einen nicht existierenden Operator zu benutzen. Nur Sie wissen, wie dieser Operator aussehen sollte, und sollte derjenige sein, der es schreibt. Siehe [Operatorüberladung] (https://stackoverflow.com/questions/4421706/operator-overloading) – WhozCraig