void print(osstream os) const ???



  • Hallo alle,

    Ich lese gerade ein Stück einfache Code von einem Buch, aber sie funktioniert irgendwie nicht. Der Compiler berichtet über Euro1_t.cpp:

    line 20: error: initializing argument 1 of `void Euro::print(std::ostream) const 😕

    Also, meine Klass Code ist

    class Euro
    {
    private:
       ...
    public:
       ...
       void print(ostream os) const
       {
           os << asString() << " Euro" << endl;
       }
    
    }
    

    meine haupt Programm code ist:

    int main()
    {
    ...
    Euro einkauf(20, 50);
    ...
    cout << "Einkaufpreis: ";
    einkauf.print(cout);  // Hier kommt den Fehler!!! Warum????
    ...
    }
    

    Die vollständige Code ist hier:
    Euro1.h

    // Euro1.h
    // Die Klasse Euro zur Darstellung eines Euro
    // mit arithmetischen Operatoren.
    // Für Beträge bis zu +- 20 Millionen Euro.
    // ---------------------------------------------------
    
    #ifndef _EURO_H_
    #define _EURO_H_
    
    #include <sstream>             // Für Klasse stringstream
    #include <iomanip>
    using namespace std;
    
    class Euro
    {
      private:
        long  data;            // Euros * 100 + Cents
    
       public:
        Euro( int euro = 0, int cents = 0)
        {
           data = 100L * (long)euro + cents;
        }
    
        Euro( double x)
        {
           x *= 100.0;                               // Runden, z.B.
           data = (long)( x >= 0.0 ? x+0.5 : x-0.5); // 9.7 -> 10
        }
    
        long getWholePart() const { return data/100; }
        int  getCents() const { return data%100; }
    
        double asDouble() const { return (double)data/100.0; }
    
        string asString() const     // Euro-Wert als String zurückgeben.
        {
           stringstream strStream;         // Stream zur Konvertierung
           long temp = data;
           if( temp < 0) { strStream << '-';  temp = -temp; }
           strStream << temp/100 << ','
                     << setfill('0') << setw(2) << temp%100;
           return strStream.str();
        }
    
        void print( ostream os) const      // Auf Stream os ausgeben.
        {
           os << asString() << " Euro" << endl;
        }
    
        // ---- Operatorfunktionen ----
    
        Euro operator-() const              // Negation (unäres Minus))
        {
           Euro temp;
           temp.data = -data;
           return  temp;
        }
    
        Euro operator+( const Euro& e2) const  // Addition.
        {
           Euro temp;
           temp.data = data + e2.data;
           return  temp;
        }
    
        Euro operator-( const Euro& e2) const  // Subtraktion.
        {
           Euro temp;
           temp.data = data - e2.data;
           return  temp;
        }
    
        Euro& operator+=( const Euro& e2)   // Euros hinzuaddieren.
        {
           data += e2.data;
           return  *this;
        }
    
        Euro& operator-=( const Euro& e2)   // Euros subtrahieren.
        {
           data -= e2.data;
           return  *this;
        }
    };
    
    #endif   // _EURO_H_
    

    main Programm

    // Euro1_t.cpp
    // Die Operatoren der Klasse Euro testen.
    // ---------------------------------------------------
    
    #include "Euro1.h"         // Definition der Klasse
    #include <iostream>
    using namespace std;
    
    int main()
    {
      cout << "  * * *  Test der Klasse Euro  * * *\n" << endl;
    
      Euro einkauf( 20,50),
           verkauf;
    
      verkauf = einkauf;      // Standard-Zuweisung
      verkauf += 9.49;        // += (Euro)9.49
    
      cout << "Einkaufspreis: ";
      einkauf.print(cout);
    
      cout << "Verkaufspreis: ";
      verkauf.print(cout);
    
      Euro rabatt( 2.10);     // double-Konstruktor
      verkauf -= rabatt;
    
      cout << "\nVerkaufspreis mit Rabatt: ";
      verkauf.print(cout);
    
      einkauf = 34.10;
      cout << "\nNeuer Einkaufspreis: ";
      einkauf.print(cout);
    
      Euro gewinn( verkauf - einkauf);  // Kopier-Konstruktor
    
      cout << "\nDer Gewinn: ";
      gewinn.print(cout);               // Negativ!
    
      verkauf = einkauf + 10.50;        // ok!
    
      return 0;
    }
    

    Dank im Voraus!



  • liegt daran, dass streams nich kopiert werden können, also ein aufruf mit call by value unmöglich -> call by reference:
    void print(ostream &os) const

    bb



  • Danke bb!
    Ich habe die print funktion so umgeschrieben, jetzt läuft das Programm gut! 😃


Anmelden zum Antworten