重载<<< toString函数的运算符

时间:2015-07-16 16:47:26

标签: c++ operator-overloading

我试图重载我的输出操作符,<<。我觉得好像我很亲密,但我得到一个无法绑定错误。下面,我有我的Operators.cpp类,我的graphcode.h和我的main.cpp,我正在尝试重载toString函数。

 #include "GraphCode.h"
    #include "Node.h"


    /****************************************************************
     * Overloaded '<<' operator
    **/
    ofstream& operator <<(ofstream &outStream, const GraphCode& graphCode)
    {
      outStream << graphCode.toString();
      return outputStream;
    }
    //*******************************************************************

    #ifndef GRAPHCODE_H
    #define GRAPHCODE_H

    #include <iostream>
    #include <vector>
    using namespace std;

    #include "../../Utilities/Utils.h"
    #include "../../Utilities/Scanner.h"
    #include "../../Utilities/ScanLine.h"

    #include "Node.h"
    #include "MyRandom.h"

    class GraphCode
    {
    public:
      GraphCode();
      virtual ~GraphCode();

      void createGraph(Scanner& inStream); // not used in this assignment
      void descendFrom(ofstream& outStream, string blanks, Node& node); // recursive function
      void doSearch(ofstream& outStream); // top level of recursive search function
      void readGraph(Scanner& inStream); // to read a graph (instead of creating one)
      string toString();

      friend ofstream& operator <<(ofstream& outStream, const GraphCode& graphCode); 

    private:
      vector<string> path;
      vector<Node> theGraph;

      // the 'blanks' is the leading string of blanks so that recursion levels can be seen
      string toStringChildren(string blanks, const vector<int>& children);
      string toStringPath(string blanks);
    };

    #endif

//***************************************************************

    int main(int argc, char *argv[])
    {
      string inFileName = "";
      string outFileName = "";
      string logFileName = "";
      ofstream outStream;

      Scanner inStream;

      GraphCode graphCode;

      Utils::CheckArgs(3, argc, argv, "infilename outfilename logfilename");

      inFileName = static_cast<string>(argv[1]); // DO NOT USE OLD CAST!
      outFileName = static_cast<string>(argv[2]);
      logFileName = static_cast<string>(argv[3]);

      Utils::LogFileOpen(logFileName);

      Utils::logStream << "Beginning execution\n";
      Utils::logStream.flush();

      Utils::FileOpen(outStream, outFileName);

      Utils::logStream << "infile  '" << inFileName << "'\n";
      Utils::logStream << "outfile '" << outFileName << "'\n";
      Utils::logStream << "logfile '" << logFileName << "'\n";

      inStream.openFile(inFileName);
    //  graphCode.createGraph(inStream);
      graphCode.readGraph(inStream);
      inStream.close();

      outStream << "RAW GRAPH\n" << graphCode << endl; //changed from  //graphCode.toString()

      outStream.flush();

      outStream << "SEARCH\n";
      graphCode.doSearch(outStream);
      outStream.flush();

      Utils::FileClose(outStream);

      Utils::logStream << "Ending execution\n";
      Utils::logStream.flush();

      Utils::FileClose(Utils::logStream);

      return 0;
    }

//Main.cpp:48:13: error: cannot bind ‘std::basic_ostream<char>’ lvalue to //‘std::basic_ostream<char>&&’
  // outStream << "RAW GRAPH\n" << graphCode << endl; //this line was //changed.....  ^

//In file included from /usr/include/c++/4.8/iostream:39:0,
//                 from Main.h:13,
//                 from Main.cpp:1:
///usr/include/c++/4.8/ostream:602:5: error:   initializing argument 1 of //‘std::basic_ostream<_CharT, _Traits>& //std::operator<<(std::basic_ostream<_CharT, _Traits>&&, const _Tp&) [with //_CharT = char; _Traits = std::char_traits<char>; _Tp = GraphCode]’
//     operator<<(basic_ostream<_CharT, _Traits>&& __os, const _Tp& __x)
//     ^

0 个答案:

没有答案