Operatoren überladen



  • Hallo, ich hab die Klasse Complex als Beispiel zum Überladen von Operatoren verwendet. Alles funktioniert soweit ganz gut. Hier mal der Quelltext

    class Complex
    {
      private:
              float Re,Im;
      public:
              Complex(float re,float im);
              Complex operator +(const float &y);
              void print();
      friend Complex operator +(const Complex &y1,const Complex &y2);
      friend ostream & operator <<(ostream & os,const Complex x);
    };
    
    #include<iostream>
    using namespace std;
    
    #include "Complex.h"
    //---------------------------------------------------------------------------
    #pragma package(smart_init)
    
    Complex::Complex(float re,float im)
    {
       Re=re;
       Im=im;
    }
    //---------------------------------------------------------------------------
    void Complex::print()
    {
      cout<<Re<<" + "<<Im<<" i"<<endl;
    }
    //---------------------------------------------------------------------------
    Complex Complex::operator +(const float &y)
    {
      Complex tmp(0,0);
      tmp.Re=Re+y;
      tmp.Im=Im;
      return tmp;
    }
    //---------------------------------------------------------------------------
    Complex operator +(const Complex &y1,const Complex &y2)
    {
      Complex tmp(0,0);
      tmp.Re=y1.Re+y2.Re;
      tmp.Im=y1.Im+y2.Im;
      return tmp;
    }
    //---------------------------------------------------------------------------
    ostream & operator <<(ostream & os,const Complex x)
    {
      os<<x.Re<<" + "<<x.Im<<" i";
      return os;
    }
    
    #include<iostream>
    using namespace std;
    
    #include"Complex.h"
    
    int main(int argc, char* argv[])
    {
       Complex a(3.2,1),b(2,3.3);
       a=a+b;
       a=a+3.2;
       cout<<a<<endl;    // das funktioniert
       cout<<a+b<<endl;  //das Funktioniert nicht
       getchar();
       return 0;
    }
    

    Jetzt meine Frage: Wie in der main Funktion zu sehen, will ich einmal eine Komplexe Zahl ausgeben und einmal eine Rechnung, die als Ergebnis ja auch eine komplexe Zahl liefert. Die Zahl geht, die Rechnung nicht.
    Weiß jemand weshalb? Rein prinzipiell muß es ja gehen, da auch std_::string beispielsweise solche Operationen zuläßt. Wie müßte ich den Quelltext abändern damit es funktioniert.

    Vilen Dank schon mal

    Ebi01





  • Der Link ist ja schon schön, aber weiter hat er mich nicht gebracht. Ich weiß ja eigentlich wie es geht. Nur halt wenn ich eine Rechnung ausgeben will tut das nicht.

    @Ramses: Klingt als ob das für dich ein ganz triviales Problem wär. laß mich doch an deinem Wissen teilhaben

    Ebi01



  • Ebi01 schrieb:

    Die Zahl geht, die Rechnung nicht.
    Weiß jemand weshalb?

    Soll das heissen dass das Programm ungenau rechnet? 😕


  • Mod

    "geht nicht" ist keine Fehlerbeschreibung.



  • Ich hab deinen Code mit VS2005EE ausprobiert

    #include<iostream>
    using namespace std;
    
    class Complex
    {
      private:
              float Re,Im;
      public:
              Complex(float re,float im);
              Complex operator +(const float &y);
              void print();
      friend Complex operator +(const Complex &y1,const Complex &y2);
      friend ostream & operator <<(ostream & os,const Complex x);
    };
    
    Complex::Complex(float re,float im)
    {
       Re=re;
       Im=im;
    }
    
    void Complex::print()
    {
      cout<<Re<<" + "<<Im<<" i"<<endl;
    }
    
    Complex Complex::operator +(const float &y)
    {
      Complex tmp(0,0);
      tmp.Re=Re+y;
      tmp.Im=Im;
      return tmp;
    }
    
    Complex operator +(const Complex &y1,const Complex &y2)
    {
      Complex tmp(0,0);
      tmp.Re=y1.Re+y2.Re;
      tmp.Im=y1.Im+y2.Im;
      return tmp;
    }
    
    ostream & operator <<(ostream & os,const Complex x)
    {
      os<<x.Re<<" + "<<x.Im<<" i";
      return os;
    }
    
    int main(int argc, char* argv[])
    {
       Complex a(3.2, 1);
       Complex b(2, 3.3);
    
       a = a + b;
       a = a + 3.2;
    
       cout << "A: " << a << endl;   
       cout << "B: " << a+b << endl;  
    
       return 0;
    }
    

    Ausgabe:

    A: 8.4 + 4.3 + i
    B: 10.4 + 7.6 + i

    ein paar Warnungen kammen weil implizite double zu float cast



    1. operator+ sollte ein Objekt vom Typ const Complex zurückgeben, um Zuweisungen an das Ergebnis zu verhindern:
    (a+b) = c; //geht, wenn das Ergebnis von a+b nicht const ist
    
    1. operator<< sollte eine reference to const nehmen (const Complex&), kein const Complex. Das spart ne unnötige Kopie.

    Ob das dein Problem löst weiß ich nicht, aus dem Stehgreif sieht dein Code für mich in Ordnung aus.


Anmelden zum Antworten