Friend Funktion außerhalb von class implementieren
-
-
-
Danke für deine schnelle Antwort. Bücher habe ich hier einige und gelesen hab ich auch schon was, steht aber leider nicht drin.
Ich verstehe Deine Antwort aber es geht mir um die explizite Schreibweise denn bei Friendfunktionen scheint das etwas anders zu sein. -
stackoverflow.com/questions/47…e-operator-for-an-ostream
Noch ein volles Beispiel
Alles anzeigenQuellcode
♥C++ -
Ich bekomme dann ständig diese Meldung:
zerm schrieb:
[Blockierte Grafik: http://img411.imageshack.us/img411/3121/bildschirmfoto20111014ul.png]
Hier auch nochmal der Gesamte Code:
</iostream>Alles anzeigenQuellcode
- #include
- using namespace std;
- class c_widerstand
- {
- private:
- float r;
- public:
- //Konstruktor
- c_widerstand();
- c_widerstand(float);
- //Methoden
- friend ostream& operator << (ostream& o,c_widerstand const& var);
- c_widerstand operator + (float);
- c_widerstand operator * (float);
- c_widerstand operator + (c_widerstand);
- c_widerstand operator || (c_widerstand);
- };
- c_widerstand::c_widerstand()
- {
- r=0;
- }
- c_widerstand::c_widerstand(float var)
- {
- r=var;
- }
- ostream& c_widerstand::operator << (ostream& o, c_widerstand const& var)
- {
- o << "Wert = " << var.r;
- return o;
- }
- c_widerstand c_widerstand::operator + (float var)
- {
- c_widerstand neu;
- neu.r=r+var;
- return (neu);
- }
- c_widerstand c_widerstand::operator * (float var)
- {
- c_widerstand neu;
- neu.r=r*var;
- return (neu);
- }
- c_widerstand c_widerstand::operator + (c_widerstand var)
- {
- c_widerstand ret;
- ret.r=r+var.r;
- return ret;
- }
- c_widerstand c_widerstand::operator || (c_widerstand var)
- {
- c_widerstand neu;
- neu.r=(var.r*r)/(var.r+r);
- return (neu);
- }
- int main ()
- {
- c_widerstand r1=1.0,r2=1.0,r3=1.0;
- c_widerstand rges=r1 || r2;
- cout << rges;
- return 0;
- }
-
apfelsaft schrieb:
Ich bekomme dann ständig diese Meldung:
Das ist EXAKT die Frage bei Stackoverflow.
Tipp:
ostream& c_widerstand::operator << (ostream& o, c_widerstand const& var)♥C++ -
Hmm dann verstehe ich die Lösung nicht?!
-
Ist operator<< nun ein friend oder ein member?
Du willst ein friend, dann lass das c_widerstand:: davor weg. Beides gleichzeitig ergibt keinen Sinn.
Das sagt auch die akzeptierte Anwort auf StackOverflow
♥C++ -
Stimmt dann gehts. Vielen Lieben Dank an Dich! Ich stand grad aufm Schlauch und muss mir jetzt das mit den Friend Funktionen nochmal etwas genauer anschaun!