2016-12-18 3 views
1
struct myType{ 
public:  

    myType operator=(const myType &value){ 
     return value; 
    }; 

}; 

myType einen Operator Überlastung hat für = aber wenn es in der JSON-Klasse bei it = js.allInfo.begin(); Compiler genannt wird wirft: „Keine tragfähige Überlastung für‚=‘“Keine tragfähige Überlastung für ‚=‘ Wenn es einen Operator

class JSON{ 

private: 
vector<myType> allInfo; 

public:  

friend ostream &operator<<(ostream &os,const JSON &js) 
{ 
    vector<myType>::iterator it; 

    for(it = js.allInfo.begin(); it != js.allInfo.end();it++){ 
     cout << "this is the info "<<(it->getNAME()) << endl; 
    } 
    return os; 
}; 

Was sollte ich bei der Überlastung ändern = beheben dieses Problem

+0

Normalerweise ist der Rückgabetyp für den Zuweisungsoperator ist ein Verweis auf dieser Typ wäre in diesem Fall 'myType &'. Sie geben 'myType' von' operator = 'und nicht' myType & 'zurück. Weiß nicht, ob dir das weiterhilft ... –

+0

Ich mache das schon const myType & value, es ist dasselbe wie const myType & value, wenn es das ist, was du meinst –

+0

Nein, 'const' ist korrekt für das Argument, aber nicht für den Rückgabetyp. –

Antwort

6

Sie versuchen, ein konstantes Objekt (const JSON & js) mit einem nicht-const iterator iterieren.

ein const iterator Verwendung:

vector<myType>::const_iterator it; 

Noch besser wäre es, verwenden Sie das Schlüsselwort „auto“, um automatisch die richtige Art zu erhalten:

auto it = js.allInfo.begin() 
+0

Noch besser, verwenden Sie ein Bereichs-for-Schleife. –

Verwandte Themen