重载<<操作员 [英] Overloading the << operator
问题描述
我正试图使<<操作员.我已经成功地使其他操作员超载,但是最后一个给我带来麻烦.也许它只需要新的眼光.我觉得这全是由const限定词引起的.
I am trying to overload the << operator. I have successfully overload the other operators but this last one is giving me trouble. Maybe it just needs a new set of eyes. I have a feeling it is all being caused by a const qualifier.
我需要操作员在哪里工作.
Where I need the operator to work.
for(UINT i = 0; i < setVector.size(); ++i)
{
outStream << "SET " << i << setVector[i] << endl;
}
其中setVector是Set类型的向量. Set是int
的向量.
where setVector is a vector of type Set. Set is a vector of int
.
这是我的operator.cpp
This is my operators.cpp
/***************************************************************************************
* Operators class
*
* Uses friend functions in order to overload the operators in the SetTester class.
*
* Author/copyright: Trevor W. Hutto All rights reserved.
* Date: 15 October 2013
*
**/
#include "Set.h"
/***************************************************************************************
* == Overloader.
*
* Overloads the == operator with the equals function in the Set class.
*
* Parameters: const Set, const Set, for the equals comparison.
* Returns: bool, if the Sets are equal.
*
**/
bool operator ==(const Set& set1, const Set& set2){
return set1.equals(set2);
}
/***************************************************************************************
* + Overloader.
*
* Overloads the + operator with the setUnion function in the Set class.
*
* Parameters: const Set, const Set, in order to find the union between the two.
* Returns: Set, the new set, displaying the union of the two parameters.
*
**/
const Set operator +(const Set& set1, const Set& set2){
return set1.setUnion(set2);
}
/***************************************************************************************
* - Overloader.
*
* Overloads the - operator with the setDifference function in the Set class.
*
* Parameters: const Set, const Set, in order to find the difference between the two.
* Returns: Set, the new set, displaying the difference of the two parameters.
*
**/
const Set operator -(const Set& set1, const Set& set2){
return set1.setDifference(set2);
}
/***************************************************************************************
* & Overloader.
*
* Overloads the & operator with the setIntersect function in the Set class.
*
* Parameters: const Set, const Set, in order to find the intersection between the two.
* Returns: Set, the new set, displaying the intersection of the two parameters.
*
**/
const Set operator &(const Set& set1, const Set& set2){
return set1.setIntersect(set2);
}
/***************************************************************************************
* << Overloader.
*
* Overloads the << operator with the toString function in the Set class.
*
* Parameters: ofstream&, const Set, the Set to call the toString on, and the ofstream
to print the string to the stream.
* Returns: ofstream&, the stream that has been written to.
*
**/
ofstream& operator <<(ofstream& outputStream, const Set& set1){
outputStream << set1.toString();
return outputStream;
}
每当使用运算符时,它都会引发这样的错误...
Whenever the operator is used, it throws errors like this...
SetTester.cpp: In member function ‘void SetTester::testSets(Scanner&, std::ofstream&)’:
SetTester.cpp:67: error: no match for ‘operator<<’ in ‘((std::basic_ostream<char, std::char_traits<char> >*)std::operator<< [with _Traits = std::char_traits<char>](((std::basic_ostream<char, std::char_traits<char> >&)(&((std::ofstream*)outStream)->std::basic_ofstream<char, std::char_traits<char> >::<anonymous>)), ((const char*)"SET ")))->std::basic_ostream<_CharT, _Traits>::operator<< [with _CharT = char, _Traits = std::char_traits<char>](i) << ((SetTester*)this)->SetTester::setVector. std::vector<_Tp, _Alloc>::operator[] [with _Tp = Set, _Alloc = std::allocator<Set>](((long unsigned int)i))’
/usr/include/c++/4.2.1/ostream:112: note: candidates are: std::basic_ostream<_CharT, _Traits>& std::basic_ostream<_CharT, _Traits>::operator<<(std::basic_ostream<_CharT, _Traits>& (*)(std::basic_ostream<_CharT, _Traits>&)) [with _CharT = char, _Traits = std::char_traits<char>]
/usr/include/c++/4.2.1/ostream:121: note: std::basic_ostream<_CharT, _Traits>& std::basic_ostream<_CharT, _Traits>::operator<<(std::basic_ios<_CharT, _Traits>& (*)(std::basic_ios<_CharT, _Traits>&)) [with _CharT = char, _Traits = std::char_traits<char>]
/usr/include/c++/4.2.1/ostream:131: note: std::basic_ostream<_CharT, _Traits>& std::basic_ostream<_CharT, _Traits>::operator<<(std::ios_base& (*)(std::ios_base&)) [with _CharT = char, _Traits = std::char_traits<char>]
/usr/include/c++/4.2.1/ostream:169: note: std::basic_ostream<_CharT, _Traits>& std::basic_ostream<_CharT, _Traits>::operator<<(long int) [with _CharT = char, _Traits = std::char_traits<char>]
/usr/include/c++/4.2.1/ostream:177: note: std::basic_ostream<_CharT, _Traits>& std::basic_ostream<_CharT, _Traits>::operator<<(long unsigned int) [with _CharT = char, _Traits = std::char_traits<char>]
/usr/include/c++/4.2.1/ostream:185: note: std::basic_ostream<_CharT, _Traits>& std::basic_ostream<_CharT, _Traits>::operator<<(bool) [with _CharT = char, _Traits = std::char_traits<char>]
/usr/include/c++/4.2.1/bits/ostream.tcc:92: note: std::basic_ostream<_CharT, _Traits>& std::basic_ostream<_CharT, _Traits>::operator<<(short int) [with _CharT = char, _Traits = std::char_traits<char>]
/usr/include/c++/4.2.1/ostream:196: note: std::basic_ostream<_CharT, _Traits>& std::basic_ostream<_CharT, _Traits>::operator<<(short unsigned int) [with _CharT = char, _Traits = std::char_traits<char>]
/usr/include/c++/4.2.1/bits/ostream.tcc:106: note: std::basic_ostream<_CharT, _Traits>& std::basic_ostream<_CharT, _Traits>::operator<<(int) [with _CharT = char, _Traits = std::char_traits<char>]
/usr/include/c++/4.2.1/ostream:207: note: std::basic_ostream<_CharT, _Traits>& std::basic_ostream<_CharT, _Traits>::operator<<(unsigned int) [with _CharT = char, _Traits = std::char_traits<char>]
/usr/include/c++/4.2.1/ostream:220: note: std::basic_ostream<_CharT, _Traits>& std::basic_ostream<_CharT, _Traits>::operator<<(long long int) [with _CharT = char, _Traits = std::char_traits<char>]
/usr/include/c++/4.2.1/ostream:228: note: std::basic_ostream<_CharT, _Traits>& std::basic_ostream<_CharT, _Traits>::operator<<(long long unsigned int) [with _CharT = char, _Traits = std::char_traits<char>]
/usr/include/c++/4.2.1/ostream:237: note: std::basic_ostream<_CharT, _Traits>& std::basic_ostream<_CharT, _Traits>::operator<<(double) [with _CharT = char, _Traits = std::char_traits<char>]
/usr/include/c++/4.2.1/ostream:245: note: std::basic_ostream<_CharT, _Traits>& std::basic_ostream<_CharT, _Traits>::operator<<(float) [with _CharT = char, _Traits = std::char_traits<char>]
/usr/include/c++/4.2.1/ostream:253: note: std::basic_ostream<_CharT, _Traits>& std::basic_ostream<_CharT, _Traits>::operator<<(long double) [with _CharT = char, _Traits = std::char_traits<char>]
/usr/include/c++/4.2.1/ostream:261: note: std::basic_ostream<_CharT, _Traits>& std::basic_ostream<_CharT, _Traits>::operator<<(const void*) [with _CharT = char, _Traits = std::char_traits<char>]
/usr/include/c++/4.2.1/bits/ostream.tcc:120: note: std::basic_ostream<_CharT, _Traits>& std::basic_ostream<_CharT, _Traits>::operator<<(std::basic_streambuf<_CharT, _Traits>*) [with _CharT = char, _Traits = std::char_traits<char>]
Set.h:35: note: std::ofstream& operator<<(std::ofstream&, const Set&)
如您所见,它有点不知所措,并且我迷失了错误消息.我想这与const有关.我试图将const移到另一端,但无济于事.
As you can see it's a bit overwhelming, and I get lost in the error message. I would guess it has something to do with the const. I have tried moving the const around, to no avail.
testSet的正文.
The body of testSets.
void SetTester::testSets(Scanner& inScanner, ofstream& outStream)
{
string line;
Set theSet; // local temp variable for input
ScanLine scanLine;
theSet = Set("");
dumpSet("NULL SET", theSet, outStream);
setVector.push_back(theSet);
theSet = Set("1 2 3 4 1 5 6 4");
dumpSet("TEST SET", theSet, outStream);
setVector.push_back(theSet);
while(inScanner.hasMoreData())
{
line = inScanner.nextLine();
theSet = Set(line);
// dumpSet("NEW SET", theSet, outStream);
setVector.push_back(theSet);
}
for(UINT i = 0; i < setVector.size(); ++i)
{
outStream << "SET " << i << setVector[i] << endl;
}
for(UINT i = 0; i < setVector.size(); ++i)
{
for(UINT j = 0; j < setVector.size(); ++j)
{
Set set1 = setVector[i];
Set set2 = setVector[j];
outStream << "SET ONE " << i << set1 << endl;
outStream << "SET TWO " << j << set2 << endl;
Set setUnion = set1 + set2;
Set setIntersection = set1 & set2;
Set setSymDiff = set1 - set2;
outStream << "SET UNION " << setUnion << endl;
outStream << "SET INTER " << setIntersection << endl;
outStream << "SET SDIFF " << setSymDiff << endl;
if(set1 == set2)
{
if(!(set1 == setUnion))
{
outStream << "ERROR: EQUAL SETS, UNION ERROR" << endl;
}
else
{
outStream << "EQUAL SETS, UNION SUCCEEDS" << endl;
}
if(!(set1 == setIntersection))
{
outStream << "ERROR: EQUAL SETS, INTERSECTION ERROR" << endl;
}
else
{
outStream << "EQUAL SETS, INTERSECTION SUCCEEDS" << endl;
}
}
outStream << endl << endl;
}
}
}
推荐答案
我不确定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)
这篇关于重载<<操作员的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!