Templateklasse im Namespace



  • Guten Abend,

    ich habe folgenden Fehler
    error: undefined reference to `cpp4cg::Vector3<float>::Vector3(float, float, float)'
    der Fehler besagt ja, dass ich die Funktion bloß deklariert und nicht definiert habe. Sie ist aber Definiert. Was mich wundert
    Hier noch der Source
    Vector3.hpp

    namespace cpp4cg {
    template<class T>
      class Vector3{
      public:
        Vector3();
        ~Vector3();
        Vector3(const Vector3& src);
        Vector3(T x, T y, T z);
        T get_x() const;
        T get_y() const;
        T get_z() const;
        void set_x(T x);
        void set_y(T y);
        void set_z(T z);
        void set(T x, T y, T z);
        double length();
        void normalize();
        Vector3 cross(const Vector3& rhs) const;
        T dot(const Vector3& rhs) const;
        Vector3 operator+(const Vector3& rhs) const;
        Vector3 operator-(const Vector3& rhs) const;
        Vector3 operator*(float rhs) const;
    
        T& operator[](int idx) const;
        T& operator[](int idx);
        Vector3& operator=(const Vector3& rhs);
        bool operator==(const Vector3& rhs);
      private:
        T data[3];
      };
    
      template<class T>
      inline Vector3<T> operator*(T lhs, const Vector3<T>& rhs);
    };
    

    reduzierte Vector3.cpp

    #include "Vector3.hpp"
    #include <math.h>
    namespace cpp4cg{
        template<class T>
         Vector3<T>::Vector3()
        {}
    
        template<class T>
          Vector3<T>::Vector3(const   Vector3<T>& src)
        {
            this->data[0] = src.get_x();
            this->data[1] = src.get_y();
            this->data[2] = src.get_z();
        }
    
        template<class T>
         Vector3<T>::Vector3(T x, T y, T z)
        {
            this->data[0] = x;
            this->data[1] = y;
            this->data[2] = z;
        }
    
        template<class T>
          Vector3<T>::~Vector3() {}
    
        template<class T>
        T   Vector3<T>::get_x() const
        {
            return this->data[0];
        }
    
        template<class T>
        T   Vector3<T>::get_y() const
        {
            return this->data[1];
        }
    
        template<class T>
        T   Vector3<T>::get_z() const
        {
            return this->data[2];
        }
      }
    }
    

    main.c

    #include <iostream>
    #include "Vector3.hpp"
    
    void print_vector(const cpp4cg::Vector3<float>& v)
    {
        std::cout << "(" << v.get_x() << " ," << v.get_y() << " ," << v.get_z() << ")" << std::endl;
    }
    
    template<class T>
    void print_vector(const cpp4cg::Vector3<T>& v)
    {
        std::cout << "(" << v.get_x() << " ," << v.get_y() << " ," << v.get_z() << ")" << std::endl;
    }
    
    int main(int argc, char** argv[])
    {
        cpp4cg::Vector3<float> Test(1,1,1);
        print_vector((Test));
        return 0;
    }
    

    Meine Frage: Wo liegt der Fehler, dass es die Definitionen nicht findet?

    mfg
    Anoni



  • http://www.c-plusplus.net/forum/246875 aus FAQ schrieb:

    Bei Templates müssen alle Definitionen in der Headerdatei erfolgen. Templates haben keine eigene Übersetzungseinheit, d.h. keine .cpp. Das liegt daran, dass zum Zeitpunkt der Instantiierung die kompletten Definitionen für den Compiler sichtbar sein müssen. Hier braucht man im Gegensatz zu nicht-Templates keine Angst vor Mehrfachdefinitionen in verschiedenen Headern zu haben, der Linker erkennt Templateklassen und -funktionen und ignoriert die zusätzlichen Definitionen.


Anmelden zum Antworten