3
votes

I have this code:

#include <iostream>
#include <vector>
#include <list>

using namespace std;

class Graph {
    public:
        //Create graph with n nodes
        Graph(int size);
             ~Graph();

        //Initialize graph
        void InitializeGraphWithRandomNum();


    private:

        vector <list <int*> > *graph;

};


void Graph::InitializeGraphWithRandomNum() {
    //Here I want to iterate
    for (int i=0; i< graph->size(); i++) {
        std::list <int*>::iterator it;
        for (it = graph[i].begin(); it< graph[i].end();++it) {
                 ..........
            }

    }
}

something wrong here. it says

No match for ‘operator=’ in ‘it = (((Graph*)this)->Graph::graph + ((unsigned int)(((unsigned int)i) * 12u)))->std::vector<_Tp, _Alloc>::begin with _Tp = std::list, _Alloc = std::allocator >, std::vector<_Tp, _Alloc>::iterator = __gnu_cxx::__normal_iterator*, std::vector > >, typename std::_Vector_base<_Tp, _Alloc>::_Tp_alloc_type::pointer = std::list*’ DejkstraAlg.cpp

Thank you Best

4

4 Answers

4
votes

graph is a pointer to a vector, not a vector. Either declare it as a vector, or use (*graph)[i].begin().

4
votes

Change to

void Graph::InitializeGraphWithRandomNum()
{
    typedef list<int*> int_list;
    typedef vector<int_list> int_list_vector;

    for (int_list_vector::iterator vectit = graph->begin(); vectit != graph->end(); ++vectit)
    {
        for (int_list::iterator listit = vectit->begin(); listit != vectit->end(); ++listit)
        {
            int* value = *listit;
        }
    }
}

Or much cleaner, if you have C++11 (not included in your tags, but might be useful to others):

void Graph::InitializeGraphWithRandomNum()
{
    for (auto vectit = graph->begin(); vectit != graph->end(); ++vectit)
    {
        for (auto listit = vectit->begin(); listit != vectit->end(); ++listit)
        {
            int* value = *listit;
        }
    }
}

And if you're into the C++11 non-member begin/end that many C++-ers (like me) are proponents of:

void Graph::InitializeGraphWithRandomNum()
{
    for (auto vectit = begin(*graph); vectit != end(*graph); ++vectit)
    {
        for (auto listit = begin(*vectit); listit != end(*vectit); ++listit)
        {
            int* value = *listit;
        }
    }
}
1
votes

graph is a pointer to vector, also to compare iterator to end() use operator!= instead of operator<

 for (it = (*graph)[i].begin(); it != (*graph)[i].end(); ++it)
 //                                ^^^

Better just write:

vector <list<int>> graph;
0
votes

The error poits to a statemenet (which you did not show) where you are trying to assign a value to an element of the list. I think you forgot that *it has type of pointer to int. So instead of

*it = value;

you have to write

**it = value;