<?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[HILFE: Template kann nicht vom Template erben, wieso?]]></title><description><![CDATA[<p>Hallo zusammen :),</p>
<p>ich übe gerade Algorithmen und habe mir vorgenommen jeden einzelnen davon in C++ zu programmieren. Damit ich auch gleichzeitig für OOA übe, mache ich das alles mit Klassen und Templates und damit es etwas schwerer wird ist das ganze auch noch verschachtelt und mit Funktionen als Parameter usw. .</p>
<p>Jetzt bin ich an einer Stelle, wo ein Template von einem Template erbt und irgendwie geht das nicht. Mir ist nicht klar wieso, vielleicht habe ich an der Stelle in der Vorlesung gepennt oder hab einfach nur was übersehen. Habe aber schon 100 Mal drüber geguckt und finde den Fehler nicht.</p>
<p>Grundaufbau:</p>
<pre><code>class SortAlgorithmHelper;

template&lt;typename T&gt;
class SortAlgorithm : public SortAlgorithmHelper

template&lt;typename T&gt;
class SelectionSort : public SortAlgorithm&lt;T&gt;
</code></pre>
<p>Klasse(HPP) SortAlgorithmHelper:</p>
<pre><code>#ifndef SORTALGORITHMHELPER_HPP
#define	SORTALGORITHMHELPER_HPP

#ifdef LINUX
#include &lt;sys/time.h&gt;
#else
#include &lt;windows.h&gt;
#endif

typedef unsigned int uint;

class SortAlgorithmHelper
{
private:
#ifdef LINUX
    double start;
    struct timeval tm;
#else
    LONGLONG g_Frequency, g_FirstCount, g_LastCount;
#endif

    uint cswap, ccomp;

    double duration, temp_duration;

protected:
    void startTimer();
    void pauseTimer();
    void resumeTimer();
    void stopTimer();

    void incrCompCounter();
    void incrSwapCounter();

public:
    SortAlgorithmHelper();

    double getDuration();

    uint getComparesAmmount();
    uint getSwapsAmmount();
};

#endif	/* SORTALGORITHM_HPP */
</code></pre>
<p>Template(HPP) SortAlgorithm:</p>
<pre><code>#ifndef SORTALGORITHM_HPP
#define	SORTALGORITHM_HPP

#include &quot;SortAlgorithmHelper.hpp&quot;

template&lt;typename T&gt;
class SortAlgorithm : public SortAlgorithmHelper
{
protected:
    uint n;
    T *a;

public:
    SortAlgorithm(uint n, T (rand_func(const T*, const uint&amp;))) : SortAlgorithmHelper(){
        this-&gt;n = n;
        this-&gt;a = SortAlgorithm::getRandArray&lt;T&gt;(n, rand_func);
    }

    SortAlgorithm(uint n, T *a) : SortAlgorithmHelper(){
        this-&gt;n = n;
        this-&gt;a = a;
    }

    virtual void sort(bool (llr(const T&amp;, const T&amp;)));

    ~SortAlgorithm(){
        delete[] this-&gt;a;
    }

public: // ---- STATIC ----
    template&lt;typename T&gt;
    static void printArray(const T *a, uint n, void (print(const T&amp;, uint&amp;)));

    template&lt;typename T&gt;
    static T* getRandArray(uint n, T (rand_func(const T*, const uint&amp;)));

    template&lt;typename T&gt;
    static void swap(T* l, T* r);
};

template&lt;typename T&gt;
void SortAlgorithm::printArray(const T* a, uint n, void (print(const T&amp;, uint&amp;))){
    for(uint i = 0; i &lt; n; i++){
        print(a[i], i);
    }
}

template&lt;typename T&gt;
T* SortAlgorithm::getRandArray(uint n, T (rand_func(const T*, const uint&amp;))){
    T *arr = new T[n];
    for(uint i = 0; i &lt; n; i++){
        arr[i] = rand_func(arr, i);
    }

    return arr;
}

template&lt;typename T&gt;
void SortAlgorithm::swap(T* l, T* r){
    T tmp = *l;
    *l = *r;
    *r = tmp;
}

#endif	/* SORTALGORITHM_HPP */
</code></pre>
<p>Template(HPP) SelectionSort:</p>
<pre><code>#ifndef SELECTIONSORT_HPP
#define	SELECTIONSORT_HPP

#include &quot;SortAlgorithm.hpp&quot;

template&lt;typename T&gt;
class SelectionSort : public SortAlgorithm&lt;T&gt;
{
public:
    SelectionSort(uint n, T (rand_func(const T*, const uint&amp;))) : SortAlgorithm(n, rand_func){

    }

    SelectionSort(uint n, T *a) : SortAlgorithm(n, a){

    }

    void sort(bool (llr(const T&amp;, const T&amp;))){ //llr is Left lower Right (x &lt; y =&gt; true)

        this-&gt;startTimer();

        for(uint i = 0; i &lt; this-&gt;n - 1; i++){
            uint k = i;
            for(uint c = i + 1; c &lt; this-&gt;n; c++){

                this-&gt;pauseTimer();
                this-&gt;incrCompCounter();
                this-&gt;resumeTimer();

                if(llr(this-&gt;a[c], this-&gt;a[k])) k = c;
            }

            if(k != i){
                this-&gt;pauseTimer();
                this-&gt;incrSwapCounter();
                this-&gt;resumeTimer();

                SortAlgorithm::swap&lt;T&gt;(&amp;this-&gt;a[i], &amp;this-&gt;a[k]);
            }
        }

        this-&gt;stopTimer();
    }
};
#endif	/* SELECTIONSORT_HPP */
</code></pre>
<p>In der SelectionSort Datei ist vieles unterstrichen von der IDE (NetBeans <img
      src="https://www.c-plusplus.net/forum/plugins/nodebb-plugin-emoji/emoji/emoji-one/1f60e.png?v=ab1pehoraso"
      class="not-responsive emoji emoji-emoji-one emoji--smiling_face_with_sunglasses"
      title="8)"
      alt="😎"
    /> und der Compiler gibt folgendes aus:</p>
<blockquote>
<p>g++ -c -g -std=c++11 -MMD -MP -MF &quot;build/Debug/Cygwin_4.x-Windows/main.o.d&quot; -o build/Debug/Cygwin_4.x-Windows/main.o main.cpp<br />
In file included from SelectionSort.hpp:4:0,<br />
from main.cpp:9:<br />
SortAlgorithm.hpp:31:14: error: declaration of ‘class T’<br />
template&lt;typename T&gt;<br />
^<br />
SortAlgorithm.hpp:6:10: error: shadows template parm ‘class T’<br />
template&lt;typename T&gt;<br />
^<br />
SortAlgorithm.hpp:34:14: error: declaration of ‘class T’<br />
template&lt;typename T&gt;<br />
^<br />
SortAlgorithm.hpp:6:10: error: shadows template parm ‘class T’<br />
template&lt;typename T&gt;<br />
^<br />
SortAlgorithm.hpp:37:14: error: declaration of ‘class T’<br />
template&lt;typename T&gt;<br />
^<br />
SortAlgorithm.hpp:6:10: error: shadows template parm ‘class T’<br />
template&lt;typename T&gt;<br />
^<br />
SortAlgorithm.hpp:43:6: error: ‘template&lt;class T&gt; class SortAlgorithm’ used without template parameters<br />
void SortAlgorithm::printArray(const T* a, uint n, void (print(const T&amp;, uint&amp;))){<br />
^<br />
SortAlgorithm.hpp:50:4: error: ‘template&lt;class T&gt; class SortAlgorithm’ used without template parameters<br />
T* SortAlgorithm::getRandArray(uint n, T (rand_func(const T*, const uint&amp;))){<br />
^<br />
SortAlgorithm.hpp:60:6: error: ‘template&lt;class T&gt; class SortAlgorithm’ used without template parameters<br />
void SortAlgorithm::swap(T* l, T* r){<br />
^<br />
SelectionSort.hpp: In member function ‘void SelectionSort&lt;T&gt;::sort(bool (<em>)(const T&amp;, const T&amp;))’:<br />
SelectionSort.hpp:30:17: error: ‘template&lt;class T&gt; class SortAlgorithm’ used without template parameters<br />
SortAlgorithm::swap&lt;T&gt;(&amp;this-&gt;a[i], &amp;this-&gt;a[k]);<br />
^<br />
SelectionSort.hpp:30:38: error: expected primary-expression before ‘&gt;’ token<br />
SortAlgorithm::swap&lt;T&gt;(&amp;this-&gt;a[i], &amp;this-&gt;a[k]);<br />
^<br />
main.cpp: In function ‘int main(int, const char**)’:<br />
main.cpp:29:39: error: ‘template&lt;class T&gt; class SortAlgorithm’ used without template parameters<br />
SelectionSort&lt;int&gt; selSort(n, SortAlgorithm::getRandArray&lt;int&gt;(n, rand_func));<br />
^<br />
main.cpp:29:67: error: expected primary-expression before ‘int’<br />
SelectionSort&lt;int&gt; selSort(n, SortAlgorithm::getRandArray&lt;int&gt;(n, rand_func));<br />
^<br />
SelectionSort.hpp: In instantiation of ‘void SelectionSort&lt;T&gt;::sort(bool (</em>)(const T&amp;, const T&amp;)) [with T = int]’:<br />
main.cpp:30:31: required from here<br />
SortAlgorithm.hpp:10:10: error: ‘uint SortAlgorithm&lt;int&gt;::n’ is private<br />
uint n;<br />
^<br />
In file included from main.cpp:9:0:<br />
SelectionSort.hpp:14:37: error: within this context<br />
for(uint i = 0; i &lt; this-&gt;n - 1; i++){<br />
^<br />
In file included from SelectionSort.hpp:4:0,<br />
from main.cpp:9:<br />
SortAlgorithm.hpp:10:10: error: ‘uint SortAlgorithm&lt;int&gt;::n’ is private<br />
uint n;<br />
^<br />
In file included from main.cpp:9:0:<br />
SelectionSort.hpp:16:35: error: within this context<br />
for(uint c = i + 1; c &lt; this-&gt;n; c++){<br />
^<br />
In file included from SelectionSort.hpp:4:0,<br />
from main.cpp:9:<br />
SortAlgorithm.hpp:11:8: error: ‘int* SortAlgorithm&lt;int&gt;::a’ is private<br />
T <em>a;<br />
^<br />
In file included from main.cpp:9:0:<br />
SelectionSort.hpp:22:31: error: within this context<br />
if(llr(this-&gt;a[c], this-&gt;a[k])) k = c;<br />
^<br />
In file included from SelectionSort.hpp:4:0,<br />
from main.cpp:9:<br />
SortAlgorithm.hpp:11:8: error: ‘int</em> SortAlgorithm&lt;int&gt;::a’ is private<br />
T <em>a;<br />
^<br />
In file included from main.cpp:9:0:<br />
SelectionSort.hpp:22:43: error: within this context<br />
if(llr(this-&gt;a[c], this-&gt;a[k])) k = c;<br />
^<br />
In file included from SelectionSort.hpp:4:0,<br />
from main.cpp:9:<br />
SortAlgorithm.hpp:11:8: error: ‘int</em> SortAlgorithm&lt;int&gt;::a’ is private<br />
T <em>a;<br />
^<br />
In file included from main.cpp:9:0:<br />
SelectionSort.hpp:30:48: error: within this context<br />
SortAlgorithm::swap&lt;T&gt;(&amp;this-&gt;a[i], &amp;this-&gt;a[k]);<br />
^<br />
In file included from SelectionSort.hpp:4:0,<br />
from main.cpp:9:<br />
SortAlgorithm.hpp:11:8: error: ‘int</em> SortAlgorithm&lt;int&gt;::a’ is private<br />
T *a;<br />
^<br />
In file included from main.cpp:9:0:<br />
SelectionSort.hpp:30:61: error: within this context<br />
SortAlgorithm::swap&lt;T&gt;(&amp;this-&gt;a[i], &amp;this-&gt;a[k]);</p>
</blockquote>
]]></description><link>https://www.c-plusplus.net/forum/topic/326182/hilfe-template-kann-nicht-vom-template-erben-wieso</link><generator>RSS for Node</generator><lastBuildDate>Sat, 11 Jul 2026 09:29:59 GMT</lastBuildDate><atom:link href="https://www.c-plusplus.net/forum/topic/326182.rss" rel="self" type="application/rss+xml"/><pubDate>Thu, 05 Jun 2014 13:05:13 GMT</pubDate><ttl>60</ttl><item><title><![CDATA[Reply to HILFE: Template kann nicht vom Template erben, wieso? on Thu, 05 Jun 2014 13:05:13 GMT]]></title><description><![CDATA[<p>Hallo zusammen :),</p>
<p>ich übe gerade Algorithmen und habe mir vorgenommen jeden einzelnen davon in C++ zu programmieren. Damit ich auch gleichzeitig für OOA übe, mache ich das alles mit Klassen und Templates und damit es etwas schwerer wird ist das ganze auch noch verschachtelt und mit Funktionen als Parameter usw. .</p>
<p>Jetzt bin ich an einer Stelle, wo ein Template von einem Template erbt und irgendwie geht das nicht. Mir ist nicht klar wieso, vielleicht habe ich an der Stelle in der Vorlesung gepennt oder hab einfach nur was übersehen. Habe aber schon 100 Mal drüber geguckt und finde den Fehler nicht.</p>
<p>Grundaufbau:</p>
<pre><code>class SortAlgorithmHelper;

template&lt;typename T&gt;
class SortAlgorithm : public SortAlgorithmHelper

template&lt;typename T&gt;
class SelectionSort : public SortAlgorithm&lt;T&gt;
</code></pre>
<p>Klasse(HPP) SortAlgorithmHelper:</p>
<pre><code>#ifndef SORTALGORITHMHELPER_HPP
#define	SORTALGORITHMHELPER_HPP

#ifdef LINUX
#include &lt;sys/time.h&gt;
#else
#include &lt;windows.h&gt;
#endif

typedef unsigned int uint;

class SortAlgorithmHelper
{
private:
#ifdef LINUX
    double start;
    struct timeval tm;
#else
    LONGLONG g_Frequency, g_FirstCount, g_LastCount;
#endif

    uint cswap, ccomp;

    double duration, temp_duration;

protected:
    void startTimer();
    void pauseTimer();
    void resumeTimer();
    void stopTimer();

    void incrCompCounter();
    void incrSwapCounter();

public:
    SortAlgorithmHelper();

    double getDuration();

    uint getComparesAmmount();
    uint getSwapsAmmount();
};

#endif	/* SORTALGORITHM_HPP */
</code></pre>
<p>Template(HPP) SortAlgorithm:</p>
<pre><code>#ifndef SORTALGORITHM_HPP
#define	SORTALGORITHM_HPP

#include &quot;SortAlgorithmHelper.hpp&quot;

template&lt;typename T&gt;
class SortAlgorithm : public SortAlgorithmHelper
{
protected:
    uint n;
    T *a;

public:
    SortAlgorithm(uint n, T (rand_func(const T*, const uint&amp;))) : SortAlgorithmHelper(){
        this-&gt;n = n;
        this-&gt;a = SortAlgorithm::getRandArray&lt;T&gt;(n, rand_func);
    }

    SortAlgorithm(uint n, T *a) : SortAlgorithmHelper(){
        this-&gt;n = n;
        this-&gt;a = a;
    }

    virtual void sort(bool (llr(const T&amp;, const T&amp;)));

    ~SortAlgorithm(){
        delete[] this-&gt;a;
    }

public: // ---- STATIC ----
    template&lt;typename T&gt;
    static void printArray(const T *a, uint n, void (print(const T&amp;, uint&amp;)));

    template&lt;typename T&gt;
    static T* getRandArray(uint n, T (rand_func(const T*, const uint&amp;)));

    template&lt;typename T&gt;
    static void swap(T* l, T* r);
};

template&lt;typename T&gt;
void SortAlgorithm::printArray(const T* a, uint n, void (print(const T&amp;, uint&amp;))){
    for(uint i = 0; i &lt; n; i++){
        print(a[i], i);
    }
}

template&lt;typename T&gt;
T* SortAlgorithm::getRandArray(uint n, T (rand_func(const T*, const uint&amp;))){
    T *arr = new T[n];
    for(uint i = 0; i &lt; n; i++){
        arr[i] = rand_func(arr, i);
    }

    return arr;
}

template&lt;typename T&gt;
void SortAlgorithm::swap(T* l, T* r){
    T tmp = *l;
    *l = *r;
    *r = tmp;
}

#endif	/* SORTALGORITHM_HPP */
</code></pre>
<p>Template(HPP) SelectionSort:</p>
<pre><code>#ifndef SELECTIONSORT_HPP
#define	SELECTIONSORT_HPP

#include &quot;SortAlgorithm.hpp&quot;

template&lt;typename T&gt;
class SelectionSort : public SortAlgorithm&lt;T&gt;
{
public:
    SelectionSort(uint n, T (rand_func(const T*, const uint&amp;))) : SortAlgorithm(n, rand_func){

    }

    SelectionSort(uint n, T *a) : SortAlgorithm(n, a){

    }

    void sort(bool (llr(const T&amp;, const T&amp;))){ //llr is Left lower Right (x &lt; y =&gt; true)

        this-&gt;startTimer();

        for(uint i = 0; i &lt; this-&gt;n - 1; i++){
            uint k = i;
            for(uint c = i + 1; c &lt; this-&gt;n; c++){

                this-&gt;pauseTimer();
                this-&gt;incrCompCounter();
                this-&gt;resumeTimer();

                if(llr(this-&gt;a[c], this-&gt;a[k])) k = c;
            }

            if(k != i){
                this-&gt;pauseTimer();
                this-&gt;incrSwapCounter();
                this-&gt;resumeTimer();

                SortAlgorithm::swap&lt;T&gt;(&amp;this-&gt;a[i], &amp;this-&gt;a[k]);
            }
        }

        this-&gt;stopTimer();
    }
};
#endif	/* SELECTIONSORT_HPP */
</code></pre>
<p>In der SelectionSort Datei ist vieles unterstrichen von der IDE (NetBeans <img
      src="https://www.c-plusplus.net/forum/plugins/nodebb-plugin-emoji/emoji/emoji-one/1f60e.png?v=ab1pehoraso"
      class="not-responsive emoji emoji-emoji-one emoji--smiling_face_with_sunglasses"
      title="8)"
      alt="😎"
    /> und der Compiler gibt folgendes aus:</p>
<blockquote>
<p>g++ -c -g -std=c++11 -MMD -MP -MF &quot;build/Debug/Cygwin_4.x-Windows/main.o.d&quot; -o build/Debug/Cygwin_4.x-Windows/main.o main.cpp<br />
In file included from SelectionSort.hpp:4:0,<br />
from main.cpp:9:<br />
SortAlgorithm.hpp:31:14: error: declaration of ‘class T’<br />
template&lt;typename T&gt;<br />
^<br />
SortAlgorithm.hpp:6:10: error: shadows template parm ‘class T’<br />
template&lt;typename T&gt;<br />
^<br />
SortAlgorithm.hpp:34:14: error: declaration of ‘class T’<br />
template&lt;typename T&gt;<br />
^<br />
SortAlgorithm.hpp:6:10: error: shadows template parm ‘class T’<br />
template&lt;typename T&gt;<br />
^<br />
SortAlgorithm.hpp:37:14: error: declaration of ‘class T’<br />
template&lt;typename T&gt;<br />
^<br />
SortAlgorithm.hpp:6:10: error: shadows template parm ‘class T’<br />
template&lt;typename T&gt;<br />
^<br />
SortAlgorithm.hpp:43:6: error: ‘template&lt;class T&gt; class SortAlgorithm’ used without template parameters<br />
void SortAlgorithm::printArray(const T* a, uint n, void (print(const T&amp;, uint&amp;))){<br />
^<br />
SortAlgorithm.hpp:50:4: error: ‘template&lt;class T&gt; class SortAlgorithm’ used without template parameters<br />
T* SortAlgorithm::getRandArray(uint n, T (rand_func(const T*, const uint&amp;))){<br />
^<br />
SortAlgorithm.hpp:60:6: error: ‘template&lt;class T&gt; class SortAlgorithm’ used without template parameters<br />
void SortAlgorithm::swap(T* l, T* r){<br />
^<br />
SelectionSort.hpp: In member function ‘void SelectionSort&lt;T&gt;::sort(bool (<em>)(const T&amp;, const T&amp;))’:<br />
SelectionSort.hpp:30:17: error: ‘template&lt;class T&gt; class SortAlgorithm’ used without template parameters<br />
SortAlgorithm::swap&lt;T&gt;(&amp;this-&gt;a[i], &amp;this-&gt;a[k]);<br />
^<br />
SelectionSort.hpp:30:38: error: expected primary-expression before ‘&gt;’ token<br />
SortAlgorithm::swap&lt;T&gt;(&amp;this-&gt;a[i], &amp;this-&gt;a[k]);<br />
^<br />
main.cpp: In function ‘int main(int, const char**)’:<br />
main.cpp:29:39: error: ‘template&lt;class T&gt; class SortAlgorithm’ used without template parameters<br />
SelectionSort&lt;int&gt; selSort(n, SortAlgorithm::getRandArray&lt;int&gt;(n, rand_func));<br />
^<br />
main.cpp:29:67: error: expected primary-expression before ‘int’<br />
SelectionSort&lt;int&gt; selSort(n, SortAlgorithm::getRandArray&lt;int&gt;(n, rand_func));<br />
^<br />
SelectionSort.hpp: In instantiation of ‘void SelectionSort&lt;T&gt;::sort(bool (</em>)(const T&amp;, const T&amp;)) [with T = int]’:<br />
main.cpp:30:31: required from here<br />
SortAlgorithm.hpp:10:10: error: ‘uint SortAlgorithm&lt;int&gt;::n’ is private<br />
uint n;<br />
^<br />
In file included from main.cpp:9:0:<br />
SelectionSort.hpp:14:37: error: within this context<br />
for(uint i = 0; i &lt; this-&gt;n - 1; i++){<br />
^<br />
In file included from SelectionSort.hpp:4:0,<br />
from main.cpp:9:<br />
SortAlgorithm.hpp:10:10: error: ‘uint SortAlgorithm&lt;int&gt;::n’ is private<br />
uint n;<br />
^<br />
In file included from main.cpp:9:0:<br />
SelectionSort.hpp:16:35: error: within this context<br />
for(uint c = i + 1; c &lt; this-&gt;n; c++){<br />
^<br />
In file included from SelectionSort.hpp:4:0,<br />
from main.cpp:9:<br />
SortAlgorithm.hpp:11:8: error: ‘int* SortAlgorithm&lt;int&gt;::a’ is private<br />
T <em>a;<br />
^<br />
In file included from main.cpp:9:0:<br />
SelectionSort.hpp:22:31: error: within this context<br />
if(llr(this-&gt;a[c], this-&gt;a[k])) k = c;<br />
^<br />
In file included from SelectionSort.hpp:4:0,<br />
from main.cpp:9:<br />
SortAlgorithm.hpp:11:8: error: ‘int</em> SortAlgorithm&lt;int&gt;::a’ is private<br />
T <em>a;<br />
^<br />
In file included from main.cpp:9:0:<br />
SelectionSort.hpp:22:43: error: within this context<br />
if(llr(this-&gt;a[c], this-&gt;a[k])) k = c;<br />
^<br />
In file included from SelectionSort.hpp:4:0,<br />
from main.cpp:9:<br />
SortAlgorithm.hpp:11:8: error: ‘int</em> SortAlgorithm&lt;int&gt;::a’ is private<br />
T <em>a;<br />
^<br />
In file included from main.cpp:9:0:<br />
SelectionSort.hpp:30:48: error: within this context<br />
SortAlgorithm::swap&lt;T&gt;(&amp;this-&gt;a[i], &amp;this-&gt;a[k]);<br />
^<br />
In file included from SelectionSort.hpp:4:0,<br />
from main.cpp:9:<br />
SortAlgorithm.hpp:11:8: error: ‘int</em> SortAlgorithm&lt;int&gt;::a’ is private<br />
T *a;<br />
^<br />
In file included from main.cpp:9:0:<br />
SelectionSort.hpp:30:61: error: within this context<br />
SortAlgorithm::swap&lt;T&gt;(&amp;this-&gt;a[i], &amp;this-&gt;a[k]);</p>
</blockquote>
]]></description><link>https://www.c-plusplus.net/forum/post/2402581</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2402581</guid><dc:creator><![CDATA[ViktorM]]></dc:creator><pubDate>Thu, 05 Jun 2014 13:05:13 GMT</pubDate></item><item><title><![CDATA[Reply to HILFE: Template kann nicht vom Template erben, wieso? on Thu, 05 Jun 2014 13:23:03 GMT]]></title><description><![CDATA[<p>Statt das zwanghaft alles als Klasse zu realisieren, solltest du es lieber vernünftig als Funktion machen.</p>
<p>Du hast kein Problem mit Templates. Das funktioniert alles. Du hast lediglich an ein paar Stellen private, wo du protected brauchst.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2402583</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2402583</guid><dc:creator><![CDATA[Marthog]]></dc:creator><pubDate>Thu, 05 Jun 2014 13:23:03 GMT</pubDate></item><item><title><![CDATA[Reply to HILFE: Template kann nicht vom Template erben, wieso? on Thu, 05 Jun 2014 15:16:02 GMT]]></title><description><![CDATA[<p>Dein Ansatz ist falsch. Du möchtest irgendwas mit Templates, Vererbung und Sortieren machen und presst deshalb deine Sortieralgorithmen in irgend eine Klassenhierarchie, die nicht sinnvoll ist. Wenn man einen Hammer hat sieht halt schnell alles wie ein Nagel aus.</p>
<p>Wie Marthog bereits gesagt hat ist es sinnvoller freie Funktionen zu verwenden, unter Verwerwendung des Iterator-Patterns. Du kannst Bubble-Sort zum Beispiel wie folgt implementieren:</p>
<pre><code>template &lt;typename Iter&gt;
void bubble_sort(Iter begin, Iter end)
{
    for ( auto i = begin; i != end; ++i )
for ( auto j = i + 1; j != end; ++j )
	    if ( *i &gt; *j )
		std::iter_swap(i,j);
}
</code></pre>
]]></description><link>https://www.c-plusplus.net/forum/post/2402591</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2402591</guid><dc:creator><![CDATA[icarus2]]></dc:creator><pubDate>Thu, 05 Jun 2014 15:16:02 GMT</pubDate></item><item><title><![CDATA[Reply to HILFE: Template kann nicht vom Template erben, wieso? on Thu, 05 Jun 2014 15:24:53 GMT]]></title><description><![CDATA[<p>Dein Ansatz ist falsch. Du möchtest irgendwas mit Templates, Vererbung und Sortieren machen und presst deshalb deine Sortieralgorithmen in irgend eine Klassenhierarchie, die nicht sinnvoll ist. Wenn man einen Hammer hat sieht halt schnell alles wie ein Nagel aus.</p>
<p>Wie Marthog bereits gesagt hat ist es sinnvoller freie Funktionen zu verwenden, unter Verwerwendung des Iterator-Patterns. Du kannst Bubble-Sort zum Beispiel wie folgt implementieren:</p>
<pre><code>template &lt;typename Iter&gt;
void bubble_sort(Iter begin, Iter end)
{
    for ( auto i = begin; i != end; ++i )
        for ( auto j = i + 1; j != end; ++j)
            if ( !(*i &lt; *j) )
                std::iter_swap(i,j);
}
</code></pre>
<p>Damit kannst du jeden Container, der entsprechende Iteratoren anbietet, sortieren.</p>
<p>Mit</p>
<pre><code>template &lt;typename Iter, typename Compare&gt;
void bubble_sort(Iter begin, Iter end, Compare cmp)
{
    for ( auto i = begin; i != end; ++i )
        for ( auto j = i + 1; j != end; ++j )
            if ( !cmp(*i,*j) )
                std::iter_swap(i,j);
}
</code></pre>
<p>kannst du zusätzlich noch eine Vergleichsfunktion angeben.</p>
<p>Als Beispielcode:</p>
<pre><code>std::vector&lt;int&gt; vec {4, 3, 9, 1};

    std::cout &lt;&lt; &quot;Ascending:\n&quot;;
    bubble_sort(vec.begin(), vec.end());
    for ( auto i : vec )
        std::cout &lt;&lt; i &lt;&lt; '\n';

    std::cout &lt;&lt; &quot;Descending:\n&quot;;
    bubble_sort(vec.begin(), vec.end(),std::greater&lt;int&gt;());
    for ( auto i : vec )
        std::cout &lt;&lt; i &lt;&lt; '\n';
</code></pre>
]]></description><link>https://www.c-plusplus.net/forum/post/2402592</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2402592</guid><dc:creator><![CDATA[icarus2]]></dc:creator><pubDate>Thu, 05 Jun 2014 15:24:53 GMT</pubDate></item><item><title><![CDATA[Reply to HILFE: Template kann nicht vom Template erben, wieso? on Thu, 05 Jun 2014 15:39:36 GMT]]></title><description><![CDATA[<p>icarus2 schrieb:</p>
<blockquote>
<pre><code>template &lt;typename Iter, typename Compare&gt;
void bubble_sort(Iter begin, Iter end, Compare cmp)
{
    for ( auto i = begin; i != end; ++i )
        for ( auto j = i + 1; j != end; ++j )
            if ( !cmp(*i,*j) )
                std::iter_swap(i,j);
}
</code></pre>
</blockquote>
<p>Du willst dich doch nicht auf Random Acces-Iteratoren beschränken, wenn das auch für Forward-Iteratoren geht.</p>
<pre><code>template &lt;typename Iter, typename Compare&gt;
void bubble_sort(Iter first, Iter last, Compare cmp = std::less&lt;void&gt;()) {
    for (auto i=first; i!=last; ++i)
        for (auto j=std::next(i); j!=end; ++j)
            if (!cmp(*i, *j))
                std::iter_swap(i, j);
}
</code></pre>
]]></description><link>https://www.c-plusplus.net/forum/post/2402595</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2402595</guid><dc:creator><![CDATA[prevers]]></dc:creator><pubDate>Thu, 05 Jun 2014 15:39:36 GMT</pubDate></item><item><title><![CDATA[Reply to HILFE: Template kann nicht vom Template erben, wieso? on Thu, 05 Jun 2014 16:01:12 GMT]]></title><description><![CDATA[<p>prevers schrieb:</p>
<blockquote>
<p>Du willst dich doch nicht auf Random Acces-Iteratoren beschränken, wenn das auch für Forward-Iteratoren geht.</p>
</blockquote>
<p><img
      src="https://www.c-plusplus.net/forum/plugins/nodebb-plugin-emoji/emoji/emoji-one/1f44d.png?v=ab1pehoraso"
      class="not-responsive emoji emoji-emoji-one emoji--thumbs_up"
      title=":+1:"
      alt="👍"
    /><br />
Habe ich in der Eile nicht darauf geachtet.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2402601</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2402601</guid><dc:creator><![CDATA[icarus2]]></dc:creator><pubDate>Thu, 05 Jun 2014 16:01:12 GMT</pubDate></item></channel></rss>