Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Debugging map insertion?

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));
    }
};
like image 595
Machinarius Avatar asked Apr 13 '26 16:04

Machinarius


1 Answers

You have a type mismatch. You're passing a Nodo* into the pair constructor while it expects a Nodo object.

You declare:

Nodo *Raiz;

and then you try to call:

pair<int, Nodo>(V, Raiz)

which expects an int and a Nodo. But you passed it int and Nodo*.

What you probably want is this:

class Grafo
{
    public:
        Nodo *Raiz;
        map<int, Nodo*> Nodos;    //  change to pointer

        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));   // change to pointer
        }
};
like image 150
Mysticial Avatar answered Apr 16 '26 06:04

Mysticial



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!