2

<< 演算子をオーバーロードしようとしています。他の演算子を正常にオーバーロードしましたが、この最後の演算子が問題を引き起こしています。たぶん、新しい目のセットが必要なだけです。const修飾子が原因であると感じています。

オペレーターが作業する必要がある場所。

  for(UINT i = 0; i < setVector.size(); ++i)
  {
    outStream << "SET " << i << setVector[i] << endl;
  }

ここで、setVector は 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 を移動しようとしましたが、役に立ちませんでした。

編集: 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;
    }
  }

} 
4

2 に答える 2