<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0"><channel><title><![CDATA[Templateklasse im Namespace]]></title><description><![CDATA[<p>Guten Abend,</p>
<p>ich habe folgenden Fehler<br />
error: undefined reference to `cpp4cg::Vector3&lt;float&gt;::Vector3(float, float, float)'<br />
der Fehler besagt ja, dass ich die Funktion bloß deklariert und nicht definiert habe. Sie ist aber Definiert. Was mich wundert<br />
Hier noch der Source<br />
Vector3.hpp</p>
<pre><code>namespace cpp4cg {
template&lt;class T&gt;
  class Vector3{
  public:
    Vector3();
    ~Vector3();
    Vector3(const Vector3&amp; 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&amp; rhs) const;
    T dot(const Vector3&amp; rhs) const;
    Vector3 operator+(const Vector3&amp; rhs) const;
    Vector3 operator-(const Vector3&amp; rhs) const;
    Vector3 operator*(float rhs) const;

    T&amp; operator[](int idx) const;
    T&amp; operator[](int idx);
    Vector3&amp; operator=(const Vector3&amp; rhs);
    bool operator==(const Vector3&amp; rhs);
  private:
    T data[3];
  };

  template&lt;class T&gt;
  inline Vector3&lt;T&gt; operator*(T lhs, const Vector3&lt;T&gt;&amp; rhs);
};
</code></pre>
<p>reduzierte Vector3.cpp</p>
<pre><code>#include &quot;Vector3.hpp&quot;
#include &lt;math.h&gt;
namespace cpp4cg{
    template&lt;class T&gt;
     Vector3&lt;T&gt;::Vector3()
    {}

    template&lt;class T&gt;
      Vector3&lt;T&gt;::Vector3(const   Vector3&lt;T&gt;&amp; src)
    {
        this-&gt;data[0] = src.get_x();
        this-&gt;data[1] = src.get_y();
        this-&gt;data[2] = src.get_z();
    }

    template&lt;class T&gt;
     Vector3&lt;T&gt;::Vector3(T x, T y, T z)
    {
        this-&gt;data[0] = x;
        this-&gt;data[1] = y;
        this-&gt;data[2] = z;
    }

    template&lt;class T&gt;
      Vector3&lt;T&gt;::~Vector3() {}

    template&lt;class T&gt;
    T   Vector3&lt;T&gt;::get_x() const
    {
        return this-&gt;data[0];
    }

    template&lt;class T&gt;
    T   Vector3&lt;T&gt;::get_y() const
    {
        return this-&gt;data[1];
    }

    template&lt;class T&gt;
    T   Vector3&lt;T&gt;::get_z() const
    {
        return this-&gt;data[2];
    }
  }
}
</code></pre>
<p>main.c</p>
<pre><code>#include &lt;iostream&gt;
#include &quot;Vector3.hpp&quot;

void print_vector(const cpp4cg::Vector3&lt;float&gt;&amp; v)
{
    std::cout &lt;&lt; &quot;(&quot; &lt;&lt; v.get_x() &lt;&lt; &quot; ,&quot; &lt;&lt; v.get_y() &lt;&lt; &quot; ,&quot; &lt;&lt; v.get_z() &lt;&lt; &quot;)&quot; &lt;&lt; std::endl;
}

template&lt;class T&gt;
void print_vector(const cpp4cg::Vector3&lt;T&gt;&amp; v)
{
    std::cout &lt;&lt; &quot;(&quot; &lt;&lt; v.get_x() &lt;&lt; &quot; ,&quot; &lt;&lt; v.get_y() &lt;&lt; &quot; ,&quot; &lt;&lt; v.get_z() &lt;&lt; &quot;)&quot; &lt;&lt; std::endl;
}

int main(int argc, char** argv[])
{
    cpp4cg::Vector3&lt;float&gt; Test(1,1,1);
    print_vector((Test));
    return 0;
}
</code></pre>
<p>Meine Frage: Wo liegt der Fehler, dass es die Definitionen nicht findet?</p>
<p>mfg<br />
Anoni</p>
]]></description><link>https://www.c-plusplus.net/forum/topic/325396/templateklasse-im-namespace</link><generator>RSS for Node</generator><lastBuildDate>Sun, 12 Jul 2026 15:19:10 GMT</lastBuildDate><atom:link href="https://www.c-plusplus.net/forum/topic/325396.rss" rel="self" type="application/rss+xml"/><pubDate>Tue, 29 Apr 2014 19:14:50 GMT</pubDate><ttl>60</ttl><item><title><![CDATA[Reply to Templateklasse im Namespace on Tue, 29 Apr 2014 19:14:50 GMT]]></title><description><![CDATA[<p>Guten Abend,</p>
<p>ich habe folgenden Fehler<br />
error: undefined reference to `cpp4cg::Vector3&lt;float&gt;::Vector3(float, float, float)'<br />
der Fehler besagt ja, dass ich die Funktion bloß deklariert und nicht definiert habe. Sie ist aber Definiert. Was mich wundert<br />
Hier noch der Source<br />
Vector3.hpp</p>
<pre><code>namespace cpp4cg {
template&lt;class T&gt;
  class Vector3{
  public:
    Vector3();
    ~Vector3();
    Vector3(const Vector3&amp; 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&amp; rhs) const;
    T dot(const Vector3&amp; rhs) const;
    Vector3 operator+(const Vector3&amp; rhs) const;
    Vector3 operator-(const Vector3&amp; rhs) const;
    Vector3 operator*(float rhs) const;

    T&amp; operator[](int idx) const;
    T&amp; operator[](int idx);
    Vector3&amp; operator=(const Vector3&amp; rhs);
    bool operator==(const Vector3&amp; rhs);
  private:
    T data[3];
  };

  template&lt;class T&gt;
  inline Vector3&lt;T&gt; operator*(T lhs, const Vector3&lt;T&gt;&amp; rhs);
};
</code></pre>
<p>reduzierte Vector3.cpp</p>
<pre><code>#include &quot;Vector3.hpp&quot;
#include &lt;math.h&gt;
namespace cpp4cg{
    template&lt;class T&gt;
     Vector3&lt;T&gt;::Vector3()
    {}

    template&lt;class T&gt;
      Vector3&lt;T&gt;::Vector3(const   Vector3&lt;T&gt;&amp; src)
    {
        this-&gt;data[0] = src.get_x();
        this-&gt;data[1] = src.get_y();
        this-&gt;data[2] = src.get_z();
    }

    template&lt;class T&gt;
     Vector3&lt;T&gt;::Vector3(T x, T y, T z)
    {
        this-&gt;data[0] = x;
        this-&gt;data[1] = y;
        this-&gt;data[2] = z;
    }

    template&lt;class T&gt;
      Vector3&lt;T&gt;::~Vector3() {}

    template&lt;class T&gt;
    T   Vector3&lt;T&gt;::get_x() const
    {
        return this-&gt;data[0];
    }

    template&lt;class T&gt;
    T   Vector3&lt;T&gt;::get_y() const
    {
        return this-&gt;data[1];
    }

    template&lt;class T&gt;
    T   Vector3&lt;T&gt;::get_z() const
    {
        return this-&gt;data[2];
    }
  }
}
</code></pre>
<p>main.c</p>
<pre><code>#include &lt;iostream&gt;
#include &quot;Vector3.hpp&quot;

void print_vector(const cpp4cg::Vector3&lt;float&gt;&amp; v)
{
    std::cout &lt;&lt; &quot;(&quot; &lt;&lt; v.get_x() &lt;&lt; &quot; ,&quot; &lt;&lt; v.get_y() &lt;&lt; &quot; ,&quot; &lt;&lt; v.get_z() &lt;&lt; &quot;)&quot; &lt;&lt; std::endl;
}

template&lt;class T&gt;
void print_vector(const cpp4cg::Vector3&lt;T&gt;&amp; v)
{
    std::cout &lt;&lt; &quot;(&quot; &lt;&lt; v.get_x() &lt;&lt; &quot; ,&quot; &lt;&lt; v.get_y() &lt;&lt; &quot; ,&quot; &lt;&lt; v.get_z() &lt;&lt; &quot;)&quot; &lt;&lt; std::endl;
}

int main(int argc, char** argv[])
{
    cpp4cg::Vector3&lt;float&gt; Test(1,1,1);
    print_vector((Test));
    return 0;
}
</code></pre>
<p>Meine Frage: Wo liegt der Fehler, dass es die Definitionen nicht findet?</p>
<p>mfg<br />
Anoni</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2396798</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2396798</guid><dc:creator><![CDATA[Anoni]]></dc:creator><pubDate>Tue, 29 Apr 2014 19:14:50 GMT</pubDate></item><item><title><![CDATA[Reply to Templateklasse im Namespace on Tue, 29 Apr 2014 19:18:53 GMT]]></title><description><![CDATA[<p><a href="http://www.c-plusplus.net/forum/246875" rel="nofollow">http://www.c-plusplus.net/forum/246875</a> aus FAQ schrieb:</p>
<blockquote>
<p>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.</p>
</blockquote>
]]></description><link>https://www.c-plusplus.net/forum/post/2396799</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2396799</guid><dc:creator><![CDATA[Nathan]]></dc:creator><pubDate>Tue, 29 Apr 2014 19:18:53 GMT</pubDate></item></channel></rss>