C++ C++;成员引用基类型';顶点*const';不是一个结构或联盟

C++ C++;成员引用基类型';顶点*const';不是一个结构或联盟,c++,vector,reference,iterator,constants,C++,Vector,Reference,Iterator,Constants,我在尝试访问存储在向量中的对象的方法时遇到了麻烦。我知道getEdges返回一个无序的边映射,但是我缺少一些关于如何从向量中引用顶点对象的内容。帮忙 无效无向图::minSpanningTree(): std::vector<Vertex*> visited; if(vertices.begin() != vertices.end()) { visited.push_back(vertices.begin()->second); visited[0]->

我在尝试访问存储在向量中的对象的方法时遇到了麻烦。我知道getEdges返回一个无序的边映射,但是我缺少一些关于如何从向量中引用顶点对象的内容。帮忙

无效无向图::minSpanningTree():

std::vector<Vertex*> visited;

if(vertices.begin() != vertices.end())
{
    visited.push_back(vertices.begin()->second);
    visited[0]->distance = 0;
}
else
{
    return;
}

std::vector<Vertex*>::const_iterator vit;
vit = visited.begin();
std::unordered_map<std::string, Edge> edges;
edges = vit -> getEdges();
return edges;
 UndirectedGraph.cpp:112:21: error: member reference base type 'Vertex
 *const' is
       not a structure or union
         edges = vit -> getEdges();
                 ~~~ ^  ~~~~~~~~ 1 error generated.
错误:

std::vector<Vertex*> visited;

if(vertices.begin() != vertices.end())
{
    visited.push_back(vertices.begin()->second);
    visited[0]->distance = 0;
}
else
{
    return;
}

std::vector<Vertex*>::const_iterator vit;
vit = visited.begin();
std::unordered_map<std::string, Edge> edges;
edges = vit -> getEdges();
return edges;
 UndirectedGraph.cpp:112:21: error: member reference base type 'Vertex
 *const' is
       not a structure or union
         edges = vit -> getEdges();
                 ~~~ ^  ~~~~~~~~ 1 error generated.
--编辑--

改变

edges = vit -> getEdges();


给了我同样的错误。

vit
是一个迭代器。迭代器的工作方式类似于指向容器元素的指针。您的容器元素类型是
Vertex*
。因此,
vit
的工作原理类似于
Vertex**

要调用给定
顶点**p
的成员函数,您必须首先访问
顶点*
。这可以通过如下方式取消对
p
的引用来实现:

(*p)
此时,您可以调用您的成员函数,如

(*p)->getEdges()
迭代器也不例外

与上述完全不同(而且是错误的)。这和

*((p)->getEdges())
p->getEdges()

*((p)->getEdges())
p->getEdges()
这是已知的不起作用


与此相关的是,如果您使用的是原始指针,那么很可能是做错了。您应该将
顶点
对象直接存储在
std::vector
中,或者使用
shared_ptr
unique_ptr
代替原始指针。

粘贴GetEdge定义…它是:返回边;边被声明为:std::无序映射边;只是我不明白他为什么要用指针?为什么不
std::vector