Elementzugriff (by reference) mit/durch operator<<



  • Guten Abend zusammen.

    Ich habe festgestellt (oder besser mein Compiler), dass für die Ausgabe der Object-Elemente via getter-Methode kein passender operator<< gefunden werden kann (Variante A).

    Variante B übersetzt ohne Beanstandung und bringt auch das gewünschte Ergebnis.

    Meine Frage an Euch: Warum ist das so? Die Typen sind identisch.

    Variante A

    std::ostream& operator<<(std::ostream& out, hm::dots::Object const& obj)
        {
            out << "[" << obj.getPosition << "; " << obj.getAcceleration << "]";
            return out;
        }
    

    Variante B

    std::ostream& operator<<(std::ostream& out, hm::dots::Object const& obj)
        {
            hm::dots::position_t const position = obj.getPosition();
            hm::dots::direction_t const acceleration = obj.getAcceleration();
            out << "[" << position << "; " << acceleration << "]";
            return out;
        }
    

    getter-Methoden (Klasse 'hm::dots::Object')

    // ...
        inline position_t const& getPosition() const { return position_; }
        inline direction_t const& getAcceleration() const { return acceleration_; }
    // ...
    

    Überladung des operators<< (Klasse 'hm::dots::Vector2d<T>')

    // ...
    template <class T>
    std::ostream& operator<<(std::ostream&, hm::dots::Vector2d<T> const&);
        // Für Debug-Zwecke und zur Ausgabe der Vektordaten auf der Konsole
    // ...
    

    Typen, Vererbungshierarchie

    typedef float real_t
    typedef hm::dots::Position2d<real_t> position_t
    typedef hm::dots::Direction2d<real_t> direction_t
    
    hm::dots::Position2d<T> --> hm::dots::Vector2d<T>
    hm::dots::Direction2d<T> --> hm::dots::Vector2d<T>
    

    Ich danke schon mal vorab für eine Erleuchtung.

    Grüße... Heiko


  • Mod

    Klammern beim Funktionsaufruf vergessen.



  • Autsch... zu spät, zuviel Kaffee und zuviele Diagramme im Kopf 🙄

    Danke dir camper, JETZT seh ich es auch (60-Watt-Erleuchtung).

    Grüße... Heiko


Anmelden zum Antworten