这段代码正在迭代向量 &path 并且仅返回内存地址而不是实际值。我尝试过使用 &和 *
void GraphTraversal::printPath(std::vector<const Node *> &path)
{
cout << "START: ";
for (auto it = path.begin(); it != path.end(); ++it)
{
cout << *it << "->";
}
cout << "END" << endl;
};
这是输出 START: 0x7ffe7bcd63b0->0x7ffe7bcd63b0->0x7ffe7bcd63b0->0x7ffe7bcd63b0->END
Node 类有两个私有变量
int nodeID;
std::set<const Edge *> outEdges;
要访问这些变量,有两个公共函数
int getNodeID() const {
return nodeID;
}
// Get the private attribute outEdges
std::set<const Edge *> getOutEdges() const {
return outEdges;
}
我尝试使用以下代码来访问 nodeID,但效果不太好
it->getNodeID();
void GraphTraversal::printPath(std::vector<const Node *> &path)
{
cout << "START: ";
for (auto it = path.begin(); it != path.end(); ++it)
{
cout << *it << "->";
}
cout << "END" << endl;
};
This is the output START: 0x7ffe7bcd63b0->0x7ffe7bcd63b0->0x7ffe7bcd63b0->0x7ffe7bcd63b0->END
The Node class has two private variables
int nodeID;
std::set<const Edge *> outEdges;
To access these, there are two public functions
int getNodeID() const {
return nodeID;
}
// Get the private attribute outEdges
std::set<const Edge *> getOutEdges() const {
return outEdges;
}
I have tried the following code to access the nodeID, but it doesn't work as well
it->getNodeID();
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我希望这能给你一个线索。由于向量中有一个指针,因此您还需要取消引用它。
因此,您需要按照 Nikolay Fomichev 的建议使用
(*it)->getNodeID()
。I hope this gives you a clue. Since you have a pointer in your vector you need to dereference it as well.
So you need to use
(*it)->getNodeID()
as Nikolay Fomichev sugessted.