调试地图插入?
我在向地图中插入条目时遇到问题。
#include <stdio.h>
#include <vector>
#include <stack>
#include <map>
using namespace std;
class Nodo
{
public:
vector<Nodo> Relaciones;
int Valor;
bool Visitado;
Nodo(int V)
{
Valor = V;
Visitado = false;
}
};
class Grafo
{
public:
Nodo *Raiz;
map<int, Nodo> Nodos;
Grafo(int V)
{
Raiz = new Nodo(V);
//Getting http://msdn.microsoft.com/en-us/library/s5b150wd(v=VS.100).aspx here
Nodos.insert(pair<int, Nodo>(V, Raiz));
}
};
I'm having an issue with inserting an entry into a Map.
#include <stdio.h>
#include <vector>
#include <stack>
#include <map>
using namespace std;
class Nodo
{
public:
vector<Nodo> Relaciones;
int Valor;
bool Visitado;
Nodo(int V)
{
Valor = V;
Visitado = false;
}
};
class Grafo
{
public:
Nodo *Raiz;
map<int, Nodo> Nodos;
Grafo(int V)
{
Raiz = new Nodo(V);
//Getting http://msdn.microsoft.com/en-us/library/s5b150wd(v=VS.100).aspx here
Nodos.insert(pair<int, Nodo>(V, Raiz));
}
};
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您的类型不匹配。您将
Nodo*
传递到pair
构造函数中,而它需要一个Nodo
对象。您声明:,
然后尝试调用:,
它需要一个
int
和一个Nodo
。但您传递了int
和Nodo*
。您可能想要的是这样的:
You have a type mismatch. You're passing a
Nodo*
into thepair
constructor while it expects aNodo
object.You declare:
and then you try to call:
which expects an
int
and aNodo
. But you passed itint
andNodo*
.What you probably want is this:
问题是
Rais
是一个指向Nodo
的指针,但您试图将其插入到从int
到Nodo< 的映射中/code> (不是从
int
到Nodo*
的映射)。尝试:
The problem is that
Rais
is a pointer toNodo
, but you are trying to insert it into a map fromint
toNodo
(not a map fromint
toNodo*
).Try:
如前所述,“new”返回指向该对象的指针。为了获取对象本身,您需要使用“*”运算符取消引用它。这就是地图无法工作的原因。
另外,如果您想将值插入到我个人认为看起来更清晰的地图中,可以这样做
As previously mentioned, 'new' returns a pointer to the object. In order to obtain the object itself, you would need to dereference it by using the '*' operator. That is why the map fails to work.
Additionally if you want to insert values into a map which I personally believe looks clearer is by doing