Overloading the << operator

那年仲夏 提交于 2020-01-04 18:19:06

问题


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;
  }

where setVector is a vector of type Set. Set is a vector of int.

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&)

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.

EDIT: 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;
    }
  }

} 

回答1:


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 "

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)



回答2:


When overload operator<< you should use base type std::ostream instead of derived std::ofstream so replace:

ofstream& operator <<(ofstream& outputStream, const Set& set1){

with

ostream& operator <<(ostream& outputStream, const Set& set1){


来源:https://stackoverflow.com/questions/19436393/overloading-the-operator

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!