0

UPDATE: The following code gives me an error

Graph.cpp: In function 'std::ostream& operator<<(std::ostream&, const Graph&)': Graph.cpp:43: error: passing 'const std::map >, std::less, std::allocator > > > >' as 'this' argument of '_Tp& std::map<_Key, _Tp, _Compare, _Alloc>::operator[](const _Key&) [with _Key = long int, _Tp = std::vector >, _Compare = std::less, _Alloc = std::allocator > > >]' discards qualifiers Graph.cpp:44: error: passing 'const std::map >, std::less, std::allocator > > > >' as 'this' argument of '_Tp& std::map<_Key, _Tp, _Compare, _Alloc>::operator[](const _Key&) [with _Key = long int, _Tp = std::vector >, _Compare = std::less, _Alloc = std::allocator > > >]' discards qualifiers make[2]: * [build/Debug/GNU-MacOSX/Graph.o] Error 1

class Graph {
public:
    Graph();
    Graph(const Graph& orig);
    virtual ~Graph();

    void clear();
    void rgg(lint, double);
    bool is_directed() { return directed; }
    friend ostream& operator<< (ostream&, const Graph&); 


private:
    map< lint, vector<lint> > adjList;
    vector< pair<double, double> > xy;
    double radius;
    MTRand Rand;
    bool directed;
};


void Graph::rgg(lint n, double r) {
    radius = r; directed = false;

    clear(); 

    for(lint i = 0; i < n; i++)
        xy.push_back(pair<double, double>(Rand.rand(), Rand.rand()));
}

ostream& operator<< (ostream& os, const Graph& inGraph) {
    for(lint i = 0; i < inGraph.nodes; i++) {
        os << i << " ";
        if( inGraph.adjList.find(i) != inGraph.adjList.end() ) {
            for(lint idx = 0; idx < (inGraph.adjList[i]).size(); idx++ )
                os << inGraph.adjList[i].at(idx) << " ";

        }
        os << endl;
    }
}

Thank you in advance,

2
  • 2
    You have a destructor and copy-constructor, but I don't see a copy assignment operator. Why? Commented Jan 15, 2011 at 9:43
  • 2
    Why have you completely changed the question? Now none of the answers already posted make sense which will completely confuse anyone reading the question in the future. Please revert your change and post a new question if you want further help. Commented Jan 15, 2011 at 10:13

3 Answers 3

4

I suspect that you mean Rand instead of MTRand:

Rand.rand()

MTRand is the name of the type. Rand is the name of the instance you created.

Sign up to request clarification or add additional context in comments.

Comments

4

Just a guess, but did you try

 xy.push_back(pair<double, double>(MTRand.rand(), MTRand.rand())

according to the declation of xy?

EDIT: seems the OP has changed it's code, now my answer does not match the new question any more. Nevertheless, hope my answer was useful.

Comments

1

The cause of your problems is that map's operator[] is a mutable operation (if the key doesn't exist, it will be added to the map).

You will have to use the iterator returned from find():

map< lint, vector<lint> >::const_iterator it = inGraph.adjList.find(i);
if (it != inGraph.adjList.end();
for(lint idx = 0; idx < it->size(); idx++ )
    os << it->at(idx) << " ";

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.