<?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[Nested Class in template]]></title><description><![CDATA[<p>Hallo, ich versuche mich gerade an Templates wieder mit einem BST, jedoch lässt sich das folgende Beispiel nicht kompilieren.</p>
<pre><code>#ifndef BINARYTREE_H
#define	BINARYTREE_H

template&lt;class T&gt;
class Node;

template&lt;class T&gt;
class BinaryTree 
{    
public:
    BinaryTree() : root(nullptr) {};
    virtual ~BinaryTree() {};

    void insert(const T data) 
    {
        Node&lt;T&gt; *newNode = new Node&lt;T&gt;(data);
        insert(root, newNode);
    }

private:
    void insert(Node&lt;T&gt; *&amp;destination, Node&lt;T&gt; *&amp;source)
    {
        if(destination) {
            source-&gt;parent = &amp;*destination;
            if(source-&gt;data &lt;= destination-&gt;data)
                insert(destination-&gt;left, source);
            else
                insert(destination-&gt;right, source);
        }
        else {
            destination = source;
        }
    }

    class Node {
    public:
       Node(const T data) 
           : data(data), parent(nullptr), left(nullptr), right(nullptr) 
       {};
       T data;
       Node *parent, *left, *right;
    }
    Node&lt;T&gt; *root;
};

template&lt;class T&gt;

};

#endif	/* BINARYTREE_H */
</code></pre>
<pre><code>BinaryTree.h:73:5: error: ‘BinaryTree&lt;T&gt;::Node’ is not a template
     Node&lt;T&gt; *root;

BinaryTree.h:24:29: error: no matching function for call to ‘BinaryTree&lt;int&gt;::insert(BinaryTree&lt;int&gt;::Node*&amp;, BinaryTree&lt;int&gt;::Node*&amp;)’
         insert(root, newNode);
...
</code></pre>
<p>Könnte mir bitte jemand sagen, wie ich eine nested class in templates implementieren kann.</p>
]]></description><link>https://www.c-plusplus.net/forum/topic/328512/nested-class-in-template</link><generator>RSS for Node</generator><lastBuildDate>Tue, 07 Jul 2026 12:21:02 GMT</lastBuildDate><atom:link href="https://www.c-plusplus.net/forum/topic/328512.rss" rel="self" type="application/rss+xml"/><pubDate>Mon, 13 Oct 2014 18:35:33 GMT</pubDate><ttl>60</ttl><item><title><![CDATA[Reply to Nested Class in template on Mon, 13 Oct 2014 18:35:33 GMT]]></title><description><![CDATA[<p>Hallo, ich versuche mich gerade an Templates wieder mit einem BST, jedoch lässt sich das folgende Beispiel nicht kompilieren.</p>
<pre><code>#ifndef BINARYTREE_H
#define	BINARYTREE_H

template&lt;class T&gt;
class Node;

template&lt;class T&gt;
class BinaryTree 
{    
public:
    BinaryTree() : root(nullptr) {};
    virtual ~BinaryTree() {};

    void insert(const T data) 
    {
        Node&lt;T&gt; *newNode = new Node&lt;T&gt;(data);
        insert(root, newNode);
    }

private:
    void insert(Node&lt;T&gt; *&amp;destination, Node&lt;T&gt; *&amp;source)
    {
        if(destination) {
            source-&gt;parent = &amp;*destination;
            if(source-&gt;data &lt;= destination-&gt;data)
                insert(destination-&gt;left, source);
            else
                insert(destination-&gt;right, source);
        }
        else {
            destination = source;
        }
    }

    class Node {
    public:
       Node(const T data) 
           : data(data), parent(nullptr), left(nullptr), right(nullptr) 
       {};
       T data;
       Node *parent, *left, *right;
    }
    Node&lt;T&gt; *root;
};

template&lt;class T&gt;

};

#endif	/* BINARYTREE_H */
</code></pre>
<pre><code>BinaryTree.h:73:5: error: ‘BinaryTree&lt;T&gt;::Node’ is not a template
     Node&lt;T&gt; *root;

BinaryTree.h:24:29: error: no matching function for call to ‘BinaryTree&lt;int&gt;::insert(BinaryTree&lt;int&gt;::Node*&amp;, BinaryTree&lt;int&gt;::Node*&amp;)’
         insert(root, newNode);
...
</code></pre>
<p>Könnte mir bitte jemand sagen, wie ich eine nested class in templates implementieren kann.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2422037</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2422037</guid><dc:creator><![CDATA[DKlay]]></dc:creator><pubDate>Mon, 13 Oct 2014 18:35:33 GMT</pubDate></item><item><title><![CDATA[Reply to Nested Class in template on Mon, 13 Oct 2014 19:10:09 GMT]]></title><description><![CDATA[<p>Da ist irgendetwas falsch gecopypasted, am Ende ist Murks.<br />
Und deine forward-declaration deklariert nicht BinaryTree&lt;T&gt;::Node, sondern Node&lt;T&gt;. Sie ist im Übrigen auch überflüssig, denn der Compiler findet BinaryTree&lt;T&gt;::Node und nicht Node&lt;T&gt; und sagt, dass die nested class kein Template ist.<br />
Schreib einfach nur Node und lösch die Forward-Deklaration und es sollte gehen.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2422042</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2422042</guid><dc:creator><![CDATA[Nathan]]></dc:creator><pubDate>Mon, 13 Oct 2014 19:10:09 GMT</pubDate></item><item><title><![CDATA[Reply to Nested Class in template on Mon, 13 Oct 2014 19:32:41 GMT]]></title><description><![CDATA[<p>Tur mir leid für den murks <img
      src="https://www.c-plusplus.net/forum/plugins/nodebb-plugin-emoji/emoji/emoji-one/1f642.png?v=ab1pehoraso"
      class="not-responsive emoji emoji-emoji-one emoji--slightly_smiling_face"
      title=":)"
      alt="🙂"
    /></p>
<pre><code>#ifndef BINARYTREE_H
#define	BINARYTREE_H

template&lt;class T&gt;
class BinaryTree 
{    
public:
    BinaryTree() : root(nullptr) {};
    virtual ~BinaryTree() {};

    void insert(T data) 
    {
        Node *newNode = new Node(data);
        insert(root, newNode);
    }

private:
    void insert(Node *&amp;destination, Node *&amp;source)
    {
        if(destination) {
            source-&gt;parent = &amp;*destination;
            if(source-&gt;data &lt;= destination-&gt;data)
                insert(destination-&gt;left, source);
            else
                insert(destination-&gt;right, source);
        }
        else {
            destination = source;
        }
    }

    class Node 
    {
    public:
        Node(T data) 
            : data(data), parent(nullptr), left(nullptr), right(nullptr) {};

        T data;
        Node *parent, *left, *right;
    };

    Node *root;
};

#endif	/* BINARYTREE_H */
</code></pre>
<p>Aber mit den Änderungen, geht es leider auch nicht.</p>
<pre><code>BinaryTree.h:31:17: error: ‘Node’ has not been declared
     void insert(Node *&amp;destination, Node *&amp;source)

BinaryTree.h:34:21: error: request for member ‘parent’ in ‘source-&gt;’, which is of non-class type ‘int’
             source-&gt;parent = &amp;*destination;

...
</code></pre>
]]></description><link>https://www.c-plusplus.net/forum/post/2422045</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2422045</guid><dc:creator><![CDATA[DKlay]]></dc:creator><pubDate>Mon, 13 Oct 2014 19:32:41 GMT</pubDate></item><item><title><![CDATA[Reply to Nested Class in template on Mon, 13 Oct 2014 21:07:55 GMT]]></title><description><![CDATA[<p>Jetzt einfach noch Node vor dem privaten insert deklarieren (oder gleich die ganze Definition verschieben).</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2422055</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2422055</guid><dc:creator><![CDATA[camper]]></dc:creator><pubDate>Mon, 13 Oct 2014 21:07:55 GMT</pubDate></item></channel></rss>