0

hey i created an abstract class called "Node" and and class NodeBlock that implements the Node class. in my main class i need to print the values that inside the NodeBlock this is some of my code for the main class:

 //receving the fasteset route using the BFS algorithm.
std::stack<Node *> fast = bfs.breadthFirstSearch(start, goal);

/*print the route*/
while (!fast.empty()) {
    cout << fast.top() << endl;
    fast.pop();
}

Node:

#include <vector>
#include "Point.h"
#include <string>

using namespace std;

/**
 * An abstract class that represent Node/Vertex of a graph the node
 * has functionality that let use him for calculating route print the
 * value it holds. etc..
 */
    class Node { 
    protected:
        vector<Node*> children;
        bool visited;
        Node* father;
        int distance;

    public:
        /**
         * prints the value that the node holds.
         */
        virtual string printValue() const = 0;

        /**
         * overloading method.
         */
        virtual string operator<<(const Node *node) const {
            return printValue();
        };

    };

NodeBlock.h:

    #ifndef ADPROG1_1_NODEBLOCK_H
#define ADPROG1_1_NODEBLOCK_H

#include "Node.h"
#include "Point.h"
#include <string>


/**
 *
 */
class NodeBlock : public Node {
private:
    Point point;

public:    
    /**
     * prints the vaule that the node holds.
     */
    ostream printValue() const override ;
};
#endif //ADPROG1_1_NODEBLOCK_H

NodeBlock.cpp:

    #include "NodeBlock.h"
using namespace std;



NodeBlock::NodeBlock(Point point) : point(point) {}

string NodeBlock::printValue() const {
    return   "("  + to_string(point.getX()) + ", " + to_string(point.getY());
}

i deleted all the unnecessary method of those class. now i'm trying to overload the << operator so when i top.() from the stack it will and assign it to the "cout" it will print the string of the point.

but my current output is: 0x24f70e0 0x24f7130 0x24f7180 0x24f7340 0x24f7500

which as you know is the address. thanks for the help

yairabr
  • 101
  • 1
  • 1
  • 6
  • 1
    See this: http://stackoverflow.com/questions/476272/how-to-properly-overload-the-operator-for-an-ostream – qxz Nov 24 '16 at 21:31
  • 1
    you need a `friend` right now :) – Jean-François Fabre Nov 24 '16 at 21:32
  • `virtual string operator<<(const Node *node) const` You are defining an operator that has a node on the left and a node pointer on the right, and returns a string. Something like this: `Node a; Node b; a << &b;` would be a legal use of this operator, though not terribly useful. You probably want something else. – n. m. could be an AI Nov 24 '16 at 21:39

1 Answers1

1

What you're looking for is a << operator that has an ostream on the left and a Node on the right, and evaluates to the same ostream. Thus, it should be defined like this (outside of the Node class):

std::ostream& operator<<(std::ostream& out, const Node& node) {
    out << node.printValue();
    return out;
}

Then you need to make sure that you're couting a Node, not a Node*:

cout << *fast.top() << endl; // dereference the pointer
qxz
  • 3,814
  • 1
  • 14
  • 29
  • when i define the custom class did you mean outside the class but inside the header? – yairabr Nov 24 '16 at 21:37
  • That function has to be in the global scope, so you'll need to declare its signature in your header and put the implementation in a source file. It doesn't matter what header/source file, as long as they get included when appropriate, of course – qxz Nov 24 '16 at 21:38
  • Well you'll want the function to be declared in whatever header `Node` is in. You could make the function `static` if you really want it in the header, or just throw it in any source file. It doesn't really matter; try it out and see if you get errors or not. – qxz Nov 24 '16 at 21:47
  • multiple definition of `operator<<(std::ostream&, Node const&)' this is the output now – yairabr Nov 24 '16 at 21:51
  • When you have "multiple definitions," it means that you have the function definition being included in multiple translation units (source files). – qxz Nov 24 '16 at 21:52
  • and no for your second question, i started study c++ a week ago – yairabr Nov 24 '16 at 21:52
  • Take a look at [What is a translation unit in C++?](http://stackoverflow.com/questions/1106149/what-is-a-translation-unit-in-c) – qxz Nov 24 '16 at 21:53
  • Immediately after the definition of class `Node` in node.h, declare the function: `std::ostream& operator<<(std::ostream& out, const Node& node);`, then inside node.cpp define the function. Alternatively, for such a simple wrapper function, you could define it in the header file, but you *must* then declare it `inline`. `inline std::ostream& operator<<(std::ostream& out, const Node& node) { return out << node.printValue(); }` – Martin Bonner supports Monica Nov 24 '16 at 21:54
  • Note: The formatting of my code is appalling - but that's just for comments. – Martin Bonner supports Monica Nov 24 '16 at 21:55
  • Basically yairabr, if you add the `inline` modifier to a function it allows it to be defined in multiple translation units and not get "duplicate" errors. (`inline` should only be used on small functions) – qxz Nov 24 '16 at 21:56
  • 1
    Have you made sure that the function is declared in an included header file? – qxz Nov 24 '16 at 21:57
  • gxz thanks a lot for the help! i really appreciate this! – yairabr Nov 24 '16 at 21:58