C++ 使用重载运算符的文件操作表达式未给出预期结果?

C++ 使用重载运算符的文件操作表达式未给出预期结果?,c++,file,C++,File,我正在尝试为在文件上操作的自定义类重载运算符“+”和“=”。 运算符“+”重载将右操作数文件的内容附加到左操作数文件。 And运算符“=”重载将右操作数文件的内容写入左操作数文件。 运算符重载可以独立工作,但当在表达式中组合时,它们不会给出预期的结果。 使用下面程序中的表达式efile3=(efile2+efile1)会导致efile3在追加之前只获取efile2的内容。 efile2正确地附加了EFIL1的内容。 为什么表达式不能给出预期的结果?它是否与运算符优先级有关 #inclu

我正在尝试为在文件上操作的自定义类重载运算符“+”和“=”。 运算符“+”重载将右操作数文件的内容附加到左操作数文件。 And运算符“=”重载将右操作数文件的内容写入左操作数文件。 运算符重载可以独立工作,但当在表达式中组合时,它们不会给出预期的结果。 使用下面程序中的表达式efile3=(efile2+efile1)会导致efile3在追加之前只获取efile2的内容。 efile2正确地附加了EFIL1的内容。 为什么表达式不能给出预期的结果?它是否与运算符优先级有关

     #include<fstream>
     #include<string>
    class EasyFile{
        std::string fileContent;
        std::string temp1,temp;
        char* filePath;
        public:
            EasyFile(char* filePath){
               this->filePath = filePath;
               std::ifstream file(filePath);
               int count=0;
               while(file){
                   getline(file,temp1);
                   count++;
               }
               count--;
               std::ifstream file1(filePath);
               while(count!=0){
                    getline(file1,temp1);
                    temp = temp + temp1+"\n";
                    count--;
               }
               setFileContent(temp);
            }

            void setFileContent(std::string line){
                fileContent = line;

            }

            char* getFilePath(){
                return filePath;
            }

            std::string getFileContent(){
                return fileContent;
            }

            void setContent(std::string content){
                std::ofstream file(filePath);
                file<<content;
            }
            void operator=(EasyFile f);
            EasyFile operator+(EasyFile f);
    };
    void EasyFile::operator=(EasyFile f){
        this->setContent(f.getFileContent());
    }
    EasyFile EasyFile::operator+(EasyFile f){
        EasyFile f1(this->getFilePath());
        std::string totalContent = f1.getFileContent()+f.getFileContent();
        f1.setContent(totalContent);
        return f1;
    }

    int main(int argc,char** argv)
    {
        EasyFile efile1(argv[1]);
        EasyFile efile2(argv[2]);
        EasyFile efile3(argv[3]);
        efile3 =(efile2+efile1);
        return 0;
    }
#包括
#包括
类易用文件{
std::字符串文件内容;
std::字符串temp1,temp;
char*filePath;
公众:
EasyFile(字符*文件路径){
此->文件路径=文件路径;
std::ifstream文件(filePath);
整数计数=0;
while(文件){
getline(文件,temp1);
计数++;
}
计数--;
std::ifstream file1(filePath);
while(计数!=0){
getline(file1,temp1);
temp=temp+temp1+“\n”;
计数--;
}
setFileContent(temp);
}
void setFileContent(标准::字符串行){
fileContent=line;
}
char*getFilePath(){
返回文件路径;
}
std::string getFileContent(){
返回文件内容;
}
void setContent(标准::字符串内容){
std::流文件(文件路径);
filegetFilePath());
std::string totalContent=f1.getFileContent()+f.getFileContent();
f1.设置内容(总内容);
返回f1;
}
int main(int argc,字符**argv)
{
EasyFile efile1(argv[1]);
EasyFile efile2(argv[2]);
EasyFile efile3(argv[3]);
efile3=(efile2+efile1);
返回0;
}
我认为您没有将数据刷新到
efile1
,这就是为什么
efile3
只获取“旧”内容的原因

编辑:设置内容时,应更新文件内容:

试试这个:

//此方法由`operator+()`
void setContent(标准::字符串内容){
std::流文件(文件路径);

文件如果有人详细解释否决票,我会很高兴地删除这个问题!谢谢!您的代码不完整,并且包含太多不相关的内容。@melpomene忽略了不相关的内容。但是不完整?两个运算符都独立运行,只是表达式失败。它不完整,因为它没有编译。@melpomene和w你得到的编译器错误是什么?因为你的评论质疑了我的完整性。谢谢!这解决了问题!非常感谢!不客气。如果你愿意,你甚至可以在线玩代码(查看“在线运行”按钮)。谢谢!我不知道这一点!
// this method is called by `operator+()`
void setContent(std::string content){
    std::ofstream file(filePath);
    file << content;
    setFileContent(content);  // this should update `fileContent`
                              // which is read by `operator=()`
                              // when it calls `setContent(f.getFileContent())`
                              // thus actually updating the left-hand side of the operation
}