2011-01-15 15 views
0

UPDATE: Der folgende Code gibt mir einen FehlerC++ Problem mit Vektor push_back

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

Vielen Dank im Voraus,

+2

Sie haben eine destructor und Copy-Konstruktor, aber ich weiß nicht, eine Kopie Zuweisungsoperator sehen. [Warum?] (Http://en.wikipedia.org/wiki/Rule_of_three_%28C%2B%2B_programming%29) – GManNickG

+2

Warum hast du die Frage komplett geändert? Nun macht keine der bereits geposteten Antworten Sinn, die jeden, der die Frage liest, in Zukunft völlig durcheinander bringen wird. Bitte setzen Sie Ihre Änderung zurück und senden Sie eine neue Frage, wenn Sie weitere Hilfe benötigen. –

Antwort

1

Die Ursache Ihrer Probleme ist, dass die Karte operator[] eine veränderbare Operation ist (wenn der Schlüssel nicht existiert, wird er zur Karte hinzugefügt).

Sie haben den Iterator verwenden zurück von 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) << " "; 
4

Ich vermute, dass Sie Rand statt MTRand bedeuten:

Rand.rand() 

MTRand ist der Name des Typs. Rand ist der Name der Instanz, die Sie erstellt haben.

4

Nur eine Vermutung, aber haben Sie versucht,

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

nach dem declation von xy?

EDIT: scheint der OP hat seinen Code geändert, jetzt meine Antwort nicht mehr die neue Frage. Trotzdem hoffe meine Antwort war nützlich.