Überladener Ausgabeoperator



  • Hi,

    jemand ne Idee wo hier der Fehler liegt? Ich such schon die ganze Zeit.
    main.cpp

    #include <iostream>
    #include <string>
    #include "bullet.hpp"
    using namespace std;
    
    int main()
    {
    
        string mystr("Hallo");
        int i(10);
        bullet mybullet(mystr, i);
        cout << mybullet << endl;
        return 0;
    
    }
    

    bullet.cpp

    #include "bullet.hpp"
    
    std::ostream& operator<<(std::ostream out, const bullet& b)
    {
        out << b.val;
        return out;
    }
    

    bullet.hpp

    #ifndef BULLET_HPP
    #define BULLET_HPP
    #include <string>
    #include <iostream>
    
    class bullet
    {
    public:
        friend std::ostream& operator<<(std::ostream out, const bullet& b);
        bullet(): val(0) {}
        bullet(const std::string& s, const int& i): str(s), val(i) {}
    
    private:
    
        int val;
        std::string str;
    };
    
    #endif // BULLET_HPP
    

    Ich bekomm da 2 compilerfehler:
    1.

    /usr/include/c++/4.7/bits/ios_base.h:788: error: 'std::ios_base::ios_base(const std::ios_base&)' is private
    
    /usr/include/c++/4.7/bits/basic_ios.h:64: error: within this context
    

    Übrigens. Wenn man "cout << mybullet << endl;" in main auskommentiert compiliert es.
    Gruß



  • out als Referenz übergeben.

    std::ostream& operator<<(std::ostream& out, const bullet& b);
    


  • Wie recht du doch hast 😃 Danke hab ich voll übersehen
    Funktioniert jetzt


Anmelden zum Antworten