且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

重载<<操作员

更新时间:2023-01-19 20:18:48

我不确定Luchian为什么删除他的答案,因为我相信这是正确的,如果他提出,我会很乐意删除它并投票支持他回来.

I'm not sure why Luchian deleted his answer, because I believe it is correct, and I will gladly delete this and up-vote his if he brings it back.

操作员对此序言的评价:

The operator evaluation of this preamble:

outStream << "SET "

正在将***运算符用于 std::ostream& operator <<(std::ostream&, const std::basic_string<....>&) ,或者可能是const char*的重载.无论有问题的运算符返回std::ostream&,您都没有为其编写任何功能.您的操作员仅在流是特定 std::ofstream(或诸如std::fstream之类的派生类)时工作.

is using the free-operator for std::ostream& operator <<(std::ostream&, const std::basic_string<....>&), or perhaps an overload for const char*. Regardless the operator in question returns a std::ostream&, for which you have provided no functionality as-written. Your operator will only work when the stream is specifically an std::ofstream (or derivative such as std::fstream).

一个重要提示,那就是问题很简单,就是将您的代码更改为此:

One significant hint this is the problem is simply changing your code to this:

outStream << "SET ONE " << i ;
outStream << set1 << endl;

如果有效,但是简单地将它们链接起来就不行了,那么您的操作员可能会受到过于严格的限制.

if that works, but simply chaining them does not, then your operator is likely restricted too severely.

也就是说,简单地将您的免费运营商实施为

That said, it would be much more robust to simply implement your free-operator as

std::ostream& operator <<(std::ostream& os, const Set& set1)