Having some trouble with array of lists

Basically, it's throwing an error at the for loop
1
2
3
4
5
6
7
 for(int i = 0; i < gSize; i++){
        cout << "Starting vertex :" << i+1;
        list<weights>::iterator it;
        for( it = graph[index].begin(); it != graph[index].end(); it++){
            cout << " to vertex:" << it -> adjVertex << " weight: " << it -> edgeWeight;
        }
    }


Error is:

1
2
weightedGraph.cpp:93: error: invalid types `std::list<weights, std::allocator<weights> >*[char*()(const char*, int)]' for array subscript
weightedGraph.cpp:93: error: invalid types `std::list<weights, std::allocator<weights> >*[char*()(const char*, int)]' for array subscript


Not sure what other parts of the code I might need to provide, basically the struct weights is:
1
2
3
4
struct weights {
    int adjVertex;
    int edgeWeight;
};


gSize is how big the array is (how many vertices)

and graph is
list<weights> *graph;

Is it not liking me using .begin() and .end() because there's an array involved? Any suggestions how I might traverse through each list?
I think you need to dereference graph[index] before you can get to the begin() / end() methods, as a pointer to a list doesn't have them.

Try graph[index]->begin() / end() instead.
graph[index] is dereferencing.

It appears to be complaining about the expression used in the subscript.

What type is index? Why aren't you using i?
Last edited on
Same error unfortunately :(
Hahah, wow, I'm sorry, I just realised that it was graph[index], index comes out of nowhere, surprised it didn't say anything. It works with graph[i]. Sorry for wasting your time.
Topic archived. No new replies allowed.