简体   繁体   English

c ++将成员函数的输出写入文本文件

[英]c++ Write the output of a member function to a text file

I am trying to write the output of a member function of my class to a text file. 我正在尝试将类的成员函数的输出写入文本文件。 I cannot seem to get my output overload operator to behave as I wish. 我似乎无法让我的输出重载运算符表现出我所希望的行为。 I naively use unidentified parameters in the line 我天真地在行中使用了不确定的参数

 outStream << myClass.myMemberFunction(x1, x2, results)

because I still haven't found any method that works without changing anything about myMemberFunction. 因为我仍然没有找到任何有效的方法,而无需更改myMemberFunction的任何内容。

Here's an example: 这是一个例子:

header file 头文件

proper include guards


class myClass {

public:

bool myMemberFunction( int& x1, int& x2, std::vector<int> results);


friend ostream &operator<< (ostream& out, myClass& Class)


};

then in 然后在

classDef source file classDef源文件

proper include files

using namespace std;
using std::vector;

bool myClass::myMemberFunction(int& x1, int& x2, vector<int> results) {

int x3;
x3 = x1 + x2;
results.push_back(x3);

return true;
};

myClass& operator<< (ostream& out, myClass& myClass) {

ofstream outStream;

outStream.open("emptyFile.txt", ios::app);

if (outStream.is_open()) {
    outStream << myClass.myMemberFunction(x1, x2, results);

important part here is that I want to output the values that are stored in the results vector 这里重要的一点是我想输出存储在结果向量中的值

    outStream.close();
}
else throw "Unable to open file";
}

Is there any way to do this without changing myMemberFunction? 有什么办法可以做到而无需更改myMemberFunction?

ofstream& operator<< (ofstream& out, const myClass& instance) {
    std::vector<int> results;
    instance.myMemberFunction(x1, x2, results); // x1 and x2 need to be defined
    for(int i : results) {
       out << i;
    }
    return out;
}

You need to create the file etc. somewhere else 您需要在其他地方创建文件等

myClass classObject; // Some instance of myClass you want to output
ofstream outStream;

outStream.open("emptyFile.txt", ios::app);

if (outStream.is_open()) {
    outStream << classObject; // You can output an instance of your class now
    outStream.close();
}
else throw "Unable to open file";

You also need to update the declaration of operator<< in the header file to return an ostream& instead of myClass& . 您还需要更新头文件中的operator<<的声明,以返回ostream&而不是myClass& What you precisely do is overload the stream operator for your class. 您确切要做的就是为您的类重载流运算符。 So when you use it with a stream this method gets called and your implementation determines what happens with the stream when you want to output an instance of your class. 因此,当您将其与流一起使用时,将调用此方法,并且您的实现将确定要输出类的实例时流所发生的情况。 So you are not supposed to open a file there. 因此,您不应该在此处打开文件。 Just output the return value of your member function to the stream and return it. 只需将成员函数的返回值输出到流中并返回即可。

EDIT: You will also have to change the signature of your member function to pass the vector by reference (otherwise you fill a copy). 编辑:您还必须更改您的成员函数的签名,以通过引用传递矢量(否则您将填写副本)。 The ints doesn't need to be passed by reference. int不需要通过引用传递。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM