C++ 我创建了一个包含3个不同贴图的类。我无法将映射迭代器分配给对象';不知什么原因,这是一张地图

C++ 我创建了一个包含3个不同贴图的类。我无法将映射迭代器分配给对象';不知什么原因,这是一张地图,c++,map,iterator,maps,C++,Map,Iterator,Maps,我在一个函数方法中传递我的类的一个实例,但是我不能为实例的映射分配映射迭代器 例如: bool Graph::isSubGraph(const Graph& g) { map<string, float>::iterator gNodeIt; map<pair<string, string>, float>::iterator gEdgeIt; cout << g.NodeAmount<<endl; //

我在一个函数方法中传递我的类的一个实例,但是我不能为实例的映射分配映射迭代器

例如:

bool Graph::isSubGraph(const Graph& g)
{
    map<string, float>::iterator gNodeIt;
    map<pair<string, string>, float>::iterator gEdgeIt;
    cout << g.NodeAmount<<endl; //this works
    gNodeIt = g.Nodes.begin(); //this doesn't work???
}
bool图::isSubGraph(const图&g)
{
迭代器gNodeIt;
迭代器gEdgeIt;

cout函数的参数声明为const-Graph&g,即它是一个常量引用

但是,您尝试使用非常量迭代器,并将begin为const对象返回的常量迭代器分配给它

按以下方式更改代码

bool Graph::isSubGraph(const Graph& g)
{
    map<string, float>::const_iterator gNodeIt;
    map<pair<string, string>, float>::iterator gEdgeIt;
    cout << g.NodeAmount<<endl; //this works
    gNodeIt = g.Nodes.begin(); //this doesn't work???
}  
bool图::isSubGraph(const图&g)
{
map::const_迭代器gNodeIt;
迭代器gEdgeIt;

cout
图形的完整定义将非常有助于您解决此问题。您应该在定义
节点的位置提供代码,以便我们查看这是否正确。
bool Graph::isSubGraph(const Graph& g)
{
    map<string, float>::const_iterator gNodeIt;
    map<pair<string, string>, float>::iterator gEdgeIt;
    cout << g.NodeAmount<<endl; //this works
    gNodeIt = g.Nodes.begin(); //this doesn't work???
}