überladene operatoren << >>



  • ich habe eine struct variable angelegt und muss diese ausgeben und eingeben das funktioniert laut nem kumpel von mir mit den überladenen operatoren "<<" und ">>" ich weiss nur nicht genau wie ich das hinkriegen soll

    meine stuct definition

    struct dat
    {
       string symbol;
       float mol;
    }
    

    soweit ich weiss seht das in etwa dann so aus

    operator << ( ???  pse ??? )
    {
       cout << pse.symbol << '\t' << pse.mol << '\n';
    }
    

    das festlegen der variable und benutzung dieser

    dat pse[113];
    
    cout << pse[x];
    


  • #include <iostream>
    #include <string>
    using namespace std;
    
    struct dat{
       string symbol;
       float mol;
    };
    
    ostream &  operator<< (ostream &stream, const dat &d){
        stream<<d.symbol<<" ";
        stream<< d.symbol<<" ";
        return stream;
    }
    
    istream &  operator>> (istream &stream, dat &d){
        cout<<"eingabe symbol"<<endl;
    	stream>>d.symbol;
    	cout<<"eingabe mol"<<endl;
        stream>> d.mol;
        return stream;
    }
    
    int main(){
    	dat mydat;
    	cin>>mydat;
    	cout<<mydat;
    }
    


  • ok nun funxts


Anmelden zum Antworten