我试图访问我的Node结构中的edges_,所以我可以做一个for-loop将边缘复制到我的拷贝构造函数的新图对象中。新图的深拷贝构造函数
我收到以下错误,当我尝试访问节点中的edges_时,我感到困惑。
tests/Graph.tem:280:24: error: ‘struct std::pair<const std::__cxx11::basic_string<char>, std::shared_ptr<gdwg::Graph<std::__cxx11::basic_string<char>, int>::Node> >’ has no member named ‘edges_’
for (auto edge: node.edges_) {
~~~~~^~~~~~
我试图做一个拷贝构造函数,深拷贝到一个新的图形对象的图形中的节点和边缘:
template <typename N, typename E>
Graph<N, E>::Graph(const Graph &g):
nodes_{g.nodes_}
{
for (auto node: g.nodes_) {
for (auto edge: node.edges_) {
}
}
}
以下是我的Graph类:
template <typename N, typename E> class Graph {
private:
struct Node;
struct Edge;
struct Node {
N val_;
int numEdges_;
int numIncomingEdges_;
std::set<std::shared_ptr<Edge>> edges_;
std::set<std::shared_ptr<Edge>> incomingEdges_;
Node() {}
Node(const N x) : val_{x} { numEdges_=0; numIncomingEdges_=0; }
void printNode(N n);
~Node();
void update();
};
struct Edge {
std::weak_ptr<Node> orig;
std::weak_ptr<Node> dest;
E val_;
Edge(std::shared_ptr<Node> o, std::shared_ptr<Node> d, E x);
Edge() {};
void printEdge();
~Edge();
};
首先,如何访问它以执行深度复制?似乎有一些ptr问题。其次,是否有一种简单的方法可以深入复制存储在节点中的边缘?
什么是Graph :: nodes_'的类型?如果它是'std :: map>',如你的错误消息所示,'node'具有类型'std :: pair > ',所以而不是'node.edges_',你应该使用'node.second-> edges_'。 –
Franck
std :: map> nodes_; –
iteong
因此,对于第一个问题,您应该用'for(auto edge:node.second-> edges_)'替换'for(auto edge:node.edges_)'。 – Franck