字符串重载运算符“>>”
string overloading operator ">>"
嘿,我创建了一个名为 "Node" 的抽象 class 和实现节点 class 的 class NodeBlock。在我的 main class 中,我需要打印 NodeBlock 中的值,这是我的一些 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();
}
节点:
#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());
}
我删除了那些class所有不需要的方法。现在我正在尝试重载 << 运算符,所以当我从堆栈中 top.() 时,它会将它分配给 "cout" 它将打印点的字符串。
但我当前的输出是:
0x24f70e0
0x24f7130
0x24f7180
0x24f7340
0x24f7500
如您所知,这是地址。感谢帮助
您要找的是一个 <<
运算符,它在左侧有一个 ostream
,在右侧有一个 Node
,并且计算结果相同 ostream
.因此,它应该这样定义(在 Node
class 之外):
std::ostream& operator<<(std::ostream& out, const Node& node) {
out << node.printValue();
return out;
}
那么您需要确保您 cout
是 Node
,而不是 Node*
:
cout << *fast.top() << endl; // dereference the pointer
嘿,我创建了一个名为 "Node" 的抽象 class 和实现节点 class 的 class NodeBlock。在我的 main class 中,我需要打印 NodeBlock 中的值,这是我的一些 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();
}
节点:
#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());
}
我删除了那些class所有不需要的方法。现在我正在尝试重载 << 运算符,所以当我从堆栈中 top.() 时,它会将它分配给 "cout" 它将打印点的字符串。
但我当前的输出是: 0x24f70e0 0x24f7130 0x24f7180 0x24f7340 0x24f7500
如您所知,这是地址。感谢帮助
您要找的是一个 <<
运算符,它在左侧有一个 ostream
,在右侧有一个 Node
,并且计算结果相同 ostream
.因此,它应该这样定义(在 Node
class 之外):
std::ostream& operator<<(std::ostream& out, const Node& node) {
out << node.printValue();
return out;
}
那么您需要确保您 cout
是 Node
,而不是 Node*
:
cout << *fast.top() << endl; // dereference the pointer