2 votes

error: invalid use of void expression

In this code I get the following error: invalid use of void expression. More precisely in the return ciclo_Euleriano(g, g.getVertices().pop_front(), g.getVertices().pop_front()); and I don't know why.

bool ciclo_Euleriano(const Grafo<T> & g, T u, T first) { //u es el vertice
    bool found = false;
    if (!aristas.empty()) {
        list<typename Grafo<T>::Arco> adyacentes = g.getAdyacentes(u);
        for (typename list<typename Grafo<T>::Arco>::iterator it = adyacentes.begin(); it != adyacentes.end() && !found; it++) {
            if (Camino_no_Usado(u, it->getAdyacente(), aristas)) {
                addArista(camino, u, it->getAdyacente(), g.getCosto(u, *it));
                eliminar(u, *it, aristas);
                found = ciclo_Euleriano(g, *it, first);
                if (!found) {
                    addArista(aristas, u, *it, g.getCosto(u, *it));
                    camino.pop_back();
                }
            }
        }
    } else
        if (u == first)
            found = true;
    return found;
}

bool ciclo_Euleriano(const Grafo<T> & g) {
    list<typename Grafo<T>::Arco> aristas = g.getAristas();
    camino.clear();
    return ciclo_Euleriano(g, g.getVertices().pop_front(), g.getVertices().pop_front());
}

Here is the getVertices() :

list<T> getVertices() const {
    list<T> vertices;
    for (typename map<T, map<T, int> >::const_iterator itV = grafo.begin(); itV != grafo.end(); itV++)
        vertices.push_front(itV->first);
    return vertices;
}

0 votes

Modrias show us the definition of getVertices() ?

0 votes

@IkerObregonReigosa is done.

3voto

PaperBirdMaster Points 24910

The type void by definition is an incomplete type, which cannot be completed. It cannot be part of expressions nor can it be instantiated.

You are calling the function ciclo_Euleriano by passing it the result of calling two functions which return void .

Since Grafo<T>::getVertices returns a std::list we can see that the function pop_front returns void :

Surely what you wanted was to obtain the heads ( front ) call ciclo_Euleriano and then remove the head element:

bool ciclo_Euleriano(const Grafo<T> & g) {
    const auto &aristas = g.getAristas();
    auto v = g.getVertices().front();

    camino.clear();
    g.getVertices().pop_front();

    return ciclo_Euleriano(g, v, v);
}

As a side note, your code would be much improved in readability using auto and some C++ utilities:

list<T> getVertices() const {
    list<T> vertices;

    std::copy(grafo.begin(), grafo.end(), std::front_inserter(vertices));
    return vertices;
}

HolaDevs.com

HolaDevs is an online community of programmers and software lovers.
You can check other people responses or create a new question if you don't find a solution

Powered by:

X