Wie schreiben friend operator



  • Mein Problem.

    Ich habe eine klasse.
    Ich mache eine //friend bool operator+(myklass &a, myklass &b);//

    Wie kann ich dann auf die map Variable zugreifen?

    Z.B:

    class bla{
    	map<string, int> *klstr;
    public:
    	bla(void);
    	friend bool operator==(bla &a, bla &b);
    };
    
    bla::bla(void){
    	klstr = new bla;
    }
    
    bool operator==(bla &a, bla &b){
    	a.*klstr.clear();//Wie muss ich jetzt das schreiben?
    			// Wenn ich noch Iteraoren habe a.*it->first?
    }
    

    Habe schon fast alles durchversucht.

    Danke fuer Hilfe... Ghost



  • Wie du es auch in der Klasse machen würdest

    a.klstr->clear();
    

    Da gibt es keine Änderungen, du musst halt über ein Objekt zugreifen (hier a, bei Memberfunktionen ist das implizit this).



  • Ging Danke.

    Nun hab ich das Naechste Problem:

    map<string, int> temp;
    	temp["."]=(a.algstr["."] * b.algstr["."]);
    

    Ich will ein int wert aus der klassen map malrechnen
    und dann dem neuen map zuortnen.

    doch beim compilieren gibt es diese meldung:

    algebrapro.cpp: In function `algebra operator*(algebra&, algebra&)':
    algebrapro.cpp:129: error: invalid types `std::map<std::string, int,
       std::less<std::string>, std::allocator<std::pair<const std::string, int> >
       >*[const char[2]]' for array subscript
    algebrapro.cpp:129: error: invalid types `std::map<std::string, int,
       std::less<std::string>, std::allocator<std::pair<const std::string, int> >
       >*[const char[2]]' for array subscript
    

    Es meint, es handelt sich hier um ein Array, wie kann ich dass verhindern?

    Nochmals Danke fuer Hilfe, Ghost



  • temp["."]=((*a.algstr)["."] * (*b.algstr)["."]);
    


  • Hat geklapt.

    Noch meine letzte frage zu den operatoren.

    ich will etwas zuweisen.
    operator=()

    doch der Compilier klagt von wegen

    error: `void operator=(algebra&, algebra&)' must be a
       nonstatic member function
    

    Ich habe schon alles moegliche an return werten versucht.
    Kann mir jemand einen Tipp geben?

    Danke, Ghost



  • Versuch's mit

    algebra& operator=(algebra&)
    
    //--------------------------------------------
    
    algebra a, b;
    
    a = b;
    // wird zu
    a.operator=(b);
    


  • Geht nicht.
    Er braucht zwei argumente.

    ich dachte mir das so, habe warscheindlich ein denkfehler:

    //in der klasse
    	map<string, int> algstr;
    	//...
    public:
    	algebra(); //Konstruktor
    	friend algebra& operator=(algebra &a, algebra &b); //hier will er
    	//...						// zwei argumente
    
    //Function
    algebra::algebra(){
    	algstr = new algebra;
    }
    
    algebra& operator=(algebra &a, algebra &b){
    	a.algstr=b.algstr;
    }
    //...
    

    Der Compiler:

    algebrapro.h:22: error: `algebra& operator=(algebra&, algebra&)' must be a
       nonstatic member function
    In file included from algebrapro.h:29,
                     from sva.cpp:1:
    algebrapro.cpp:193: error: `algebra& operator=(algebra&, algebra&)' must be a
       nonstatic member function
    

    *gruebel*



  • entweder

    class algebra
    {
    // ...
    public:
      algebra& operator=(algebra& other);
    };
    

    oder

    class algebra
    {
    // ...
    public:
      friend algebra& operator=(algebra& lhs, algebra& rhs);
    };
    
    algebra& operator=(algebra& lhs, algebra& rhs)
    {
      // ...
    
    }
    

    btw:

    algebra::algebra(){
        algstr = new algebra; // <--- funktioniert nicht, lass die Zeile einfach weg
                              //      (und schau dir an wozu new ueberhaupt gut ist)
    }
    

Anmelden zum Antworten