Deklaration eines friend-Operators zwischen Templates



  • Hi,

    ich habe ein Matrix-Klasse und eine Vector-Klasse (beide Templates), und moechte einen Multiplations-operator zwischen beiden definieren. Bisher habe ich mit MS VC 6.0 und gcc 3.3 gearbeitet und hatte eine unbefriedigende Loesung:

    #ifdef WIN32
        friend Vector<T> operator* (const Matrix<T>& m, const Vector<T>& v);    
        friend Vector<T> operator* (const Vector<T>& v, const Matrix<T>& m);    
    #else
        friend Vector<T> operator*<T> (const Matrix<T>& m, const Vector<T>& v);        
        friend Vector<T> operator*<T> (const Vector<T>& v, const Matrix<T>& m);        
    #endif
    

    Jetzt bin ich auf gcc 4.0 umgestiegen und von den obigen Versionen wird keine mehr genommen. Die obere Version liefert die Warnung

    Vector.h:88: warning: friend declaration ‘Vector<T> operator*(const Matrix<T>&, const Vector<T>&)’ declares a non-template function
    Vector.h:88: warning: (if this is not what you intended, make sure the function template has already been declared and add <> after the function name here) -Wno-non-template-friend disables this warning
    Vector.h:89: warning: friend declaration ‘Vector<T> operator*(const Vector<T>&, const Matrix<T>&)’ declares a non-template function

    die untere Version den Fehler
    Vector.h:91: error: declaration of ‘operator*’ as non-function
    Vector.h:91: error: expected ‘;’ before ‘<’ token
    Vector.h:92: error: declaration of ‘operator*’ as non-function
    Vector.h:92: error: expected ‘;’ before ‘<’ token

    Jetzt Frage ich mich: gibt es denn eine voellig standard-komforme Version die ueberall ohne Fehler oder Warnung laeuft?



  • Versuch mal

    friend Vector<T> operator*<>(...);
    

    Ach, ich vergass: friend Deklaration ist keine gültige Vorwärts-Deklaration nach Standard (und GCC 4 hält sich dran). IMHO muss beim Auftauchen der friend Deklaration bereits

    template<class T>
    Vector<T> operator*(const Vector<T>&, const Matrix<T>&);
    

    bekannt sein...


  • Mod

    evtl.

    template<typename T> class Vector;
    template<typename T> class Matrix;
    
    template<typename T> Vector<T> operator* (const Vector<T>& lhs, const Matrix<T>& rhs);
    template<typename T> Vector<T> operator* (const Matrix<T>& lhs, const Vector<T>& rhs);
    
    template<typename T> class Vector
    {
    	friend Vector<T> operator*<> (const Vector<T>& lhs, const Matrix<T>& rhs);
    	friend Vector<T> operator*<> (const Matrix<T>& lhs, const Vector<T>& rhs);
    };
    
    template<typename T> class Matrix
    {
    	friend Vector<T> operator*<> (const Vector<T>& lhs, const Matrix<T>& rhs);
    	friend Vector<T> operator*<> (const Matrix<T>& lhs, const Vector<T>& rhs);
    };
    

    wenn ms vc 6.0 etwas nicht annimmt, hat das häufig genug nichts mit fehlerhaften code zu tun...





  • Vielen Dank erstmal,

    funktioniert aber trotzdem nicht.

    friend Vector<T> operator*<> (const Vector<T>& lhs, const Matrix<T>& rhs);
        friend Vector<T> operator*<> (const Matrix<T>& lhs, const Vector<T>& rhs);
    

    führt leider zur Fehlermeldung

    Vector.h:91: error: declaration of ‘operator*’ as non-function
    Vector.h:91: error: expected ‘;’ before ‘<’ token

    die Version

    friend Vector<T> operator*<T> (const Vector<T>& lhs, const Matrix<T>& rhs);
        friend Vector<T> operator*<T> (const Matrix<T>& lhs, const Vector<T>& rhs);
    

    bingt übrigens denselben Fehler 😮


Anmelden zum Antworten