эй я создал абстрактный класс под названием «Node» и класс NodeBlock, который реализует класс Node. в моем главном классе мне нужно напечатать значения, что внутри NodeBlock это часть моего кода для основного класса:оператор перегрузки строк «>>»
//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 top.() из стека он будет и назначит его «cout», он напечатает строку точки.
но мой выходной ток: 0x24f70e0 0x24f7130 0x24f7180 0x24f7340 0x24f7500
, который, как вы знаете адрес. спасибо за помощь
Смотреть это : http://stackoverflow.com/questions/476272/how-to-properly-overload-the-operator-for-an-ostream – qxz
вам нужен «друг» прямо сейчас :) –
'virtual string operator << (const Node * node) const 'Вы определяете оператор, который имеет узел слева и указатель узла справа, и возвращает строку. Что-то вроде этого: 'Node a; Узел b; a << & b; 'будет законным использованием этого оператора, хотя и не очень полезно. Вы, вероятно, хотите что-то еще. –