Machinarius
Machinarius

Reputation: 3731

Debugging vector retrieval?

This is pretty much a followup from debugging a map insertion, I applied the suggestion pointed there, but now that even more pointers were needed I'm getting really puzzled by this:

#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;
    }

    Nodo()
    {
        Visitado = false;
    }
};

class Grafo
{
public:
    Nodo *Raiz;
    map<int, Nodo*> Nodos;

    Grafo(int V)
    {
        Raiz = new Nodo(V);
        Nodos.insert(pair<int, Nodo*>(V, Raiz));
    }

    void Insertar(int _1, int _2)
    {
        Nodo *Fuente = Nodos[_1];
        Nodo *Destino = new Nodo(_2);
        Nodos.insert(pair<int, Nodo>(_2, Destino));
        Fuente->Relaciones.push_back(Destino);
        Destino->Relaciones.push_back(Fuente);
    }

    pair<int, Nodo> Resultado;
    void DFS(Nodo Fuente)
    {
        stack<pair<int, Nodo>> St;
        St.push(pair<int, Nodo>(0, Fuente));
        Fuente.Visitado = true;
        while(!St.empty())
        {
            pair<int, Nodo> P = St.top();
            int Dist = P.first;
            Nodo N = P.second;
            St.pop();
            if(Dist < Resultado.first)
            {
                Resultado.first = Dist;
                Resultado.second = N;
            }
            for(int i = 0; i < N.Relaciones.size(); i++)
            {
                //Getting error C2664: 'Nodo::Nodo(int)' : cannot convert parameter 1 from 'Nodo *' to 'int' here
                Nodo *I = N.Relaciones[i];
                if(!I->Visitado)
                {
                    I->Visitado = true;
                    St.push(pair<int, Nodo>(Dist + 1, I));
                }
            }
        }
    }

    int Procesar()
    {
        DFS(*Raiz);
        Resultado.first = 0;
        DFS(Resultado.second);
        return Resultado.first;
    }
};

int main()
{
    Grafo* G;
    int Nodos = 0;
    scanf("%d", &Nodos);
    int _1, _2 = 0;
    scanf("%d", &_1);
    scanf("%d", &_2);
    G = new Grafo(_1);
    G->Insertar(_1, _2);
    Nodos--;
    while(Nodos - 1 > 0)
    {
        scanf("%d", &_1);
        scanf("%d", &_2);
        G->Insertar(_1, _2);
        Nodos--;
    }
    printf("%d" + G->Procesar());
    system("PAUSE");
}

Shouldn't it work as it is? I declare I as a pointer to Nodo, and the [] operator is meant to give me just that, a Nodo pointer.

If it is of importance, I'm using Visual Studio 2011 configured for no charset.

Upvotes: 0

Views: 97

Answers (2)

Lou
Lou

Reputation: 1955

I see a few problems with Insertar...

    void Insertar(int _1, int _2)
    {
        Nodo *Fuente = Nodos[_1];
        Nodo *Destino = new Nodo(_2);
        Nodos.insert(pair<int, Nodo>(_2, Destino));
        Fuente->Relaciones.push_back(Destino);
        Destino->Relaciones.push_back(Fuente);
    }

1) Nodos[_1] is not the way to find the entry associated with key _1 because it will create an entry in your map if one does not already exist. Please use map::find().

2) The insert of pair is being passed a Nodo*. That is incorrect.

I only looked at this function. I'm sure there are other issues.

Upvotes: 1

Daniel
Daniel

Reputation: 31579

Since the map uses pointer, you need to insert a pair with pointer, change all

pair<int, Nodo>

to

pair<int, Nodo*>

Upvotes: 1

Related Questions