cout << Operator überladen



  • Hallo,

    wollte den << Operator überladen, aber da kommt ne Fehlermeldung, mit der ich nicht weiterkomme, ich weiß einfach nicht mehr weiter!

    operator << ist im Typ MyOutput für Argumente des Typ ostream nicht implementiert
    

    Das Hauptprogramm

    #include <vcl.h>
    #include <iostream.h>
    #include <conio.h>
    #include "MyOutput.h"
    
    void main ()
    {
        MyOutput Modul1;
        Modul1.SetZahl1(2);
        Modul1.SetZahl2(4);
        cout << Modul1;
    }
    

    Die Header-Datei

    #include <vcl.h>
    
    class MyOutput
    {
        private:
        int Zahl1;
        int Zahl2;
    
        public:
        void SetZahl1 (int);
        void SetZahl2 (int);
        void operator << (MyOutput);
    };
    

    Die Klassendefinition:

    #include "MyOutput.h"
    #include <iostream.h>
    
    void MyOutput::SetZahl1 (int Zahl1)
    {
        this->Zahl1 = Zahl1;
    }
    
    void MyOutput::SetZahl2 (int Zahl2)
    {
        this->Zahl2 = Zahl2;
    }
    
    void MyOutput::operator << (MyOutput Modul1)
    {
        cout << Modul1.Zahl1 << endl;
        cout << Modul1.Zahl2 << endl;    
    }
    


  • das musst du anders machen...

    friend std::ostream& operator<<(std::ostream& , MyOutput&);
    .
    .
    .

    std::ostream& operator<<(std::ostream& os,MyOutput& m){
    os<<m.Zahl1<<"\t"<<m.Zahl2<<std::endl;
    return os;
    }



  • friend ostream& operator << (ostream& os, const class_name &z);

    ostream& operator << (ostream& os, const class_name &z)
    {
    if(z)
    {
    os<<"bla"<<z;
    }
    return os;
    }


Anmelden zum Antworten