我正在尝试重载 << 运算符。我已经成功地重载了其他运算符,但最后一个给我带来了麻烦。也许它只是需要一双新的眼睛。我有一种感觉,这一切都是由 const 限定符引起的。
我需要操作员工作的地方。
for(UINT i = 0; i < setVector.size(); ++i)
{
outStream << "SET " << i << setVector[i] << endl;
}
其中 setVector 是 Set 类型的向量。Set 是 的向量int
。
这是我的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;
}
每当使用运算符时,它都会抛出这样的错误......
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 ,但无济于事。
编辑:测试集的主体。
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;
}
}
}