嘿,我创建了一个名为" Node"的抽象类。以及实现Node类的NodeBlock类。在我的主类中,我需要打印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();
}
节点:
#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());
}
我删除了那些类的所有不必要的方法。现在我试图重载&lt;&lt;运算符所以当我从堆栈顶部。()它将并将其分配给&#34; cout&#34;它会打印该点的字符串。
但我目前的输出是: 0x24f70e0 0x24f7130 0x24f7180 0x24f7340 0x24f7500
你知道的是地址。谢谢你的帮助
答案 0 :(得分:1)
您正在寻找的是ostream
运算符,其左侧为Node
,右侧为ostream
,并且评估为Node
std::ostream& operator<<(std::ostream& out, const Node& node) {
out << node.printValue();
return out;
}
}。因此,它应该像这样定义(在cout
类之外):
Node
然后你需要确保Node*
cout << *fast.top() << endl; // dereference the pointer
而不是DATETIME
:
MariaDB [test]> create table t (t timestamp, d datetime);
Query OK, 0 rows affected (0.59 sec)
MariaDB [test]> insert into t values ('2850-12-01 00:00:00','2850-12-01 00:00:00');
Query OK, 1 row affected, 1 warning (0.08 sec)
MariaDB [test]> select * from t;
+---------------------+---------------------+
| t | d |
+---------------------+---------------------+
| 0000-00-00 00:00:00 | 2850-12-01 00:00:00 |
+---------------------+---------------------+
1 row in set (0.00 sec)