<?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[[gelöst]template Funktion wird vom Linker nicht erkannt]]></title><description><![CDATA[<p>Hallo,</p>
<p>Ich hab mir eine kleine statische lib geschrieben, in der ich zwei Funktionen definiert habe. Eine die eine Pseudo-Zufallszahl zwischen 2 Werten zurückgibt und eine die einen Wert aus einem Array ( als template-Funktion ) zurückgibt.</p>
<p>Folgendes ist der Code dazu</p>
<pre><code>// random.h

int random( int min, int max ); // Pseudo-Zufallszahl erstellen

template&lt;class T&gt;
T randOfArr( T* arr, int size ); // Zufälliges Element aus Array auswählen
</code></pre>
<pre><code>// random.cpp

#include &lt;ctime&gt;
#include &lt;cstdlib&gt;
#include &quot;random.h&quot;

using namespace std;

bool doonce = true; // Damit srand nur einmal initialisiert wird

int random( int min, int max ) {
    if( doonce ) {
        srand( time(0) );
        doonce = false;
    }
    return (rand() % max + min);
}

template&lt;class T&gt;
T randOfArr( T* arr, int size ) {
    if( doonce ) {
        srand( time(0) );
        doonce = false;
    }
    return arr[rand()%size];
}
</code></pre>
<p>Dies lässt sich ohne Weiteres zu einer .lib kompilieren.<br />
In einem Dev-Cpp Projekt habe ich nun die header-Datei included und die lib dem<br />
Linker hinzugefügt.</p>
<p>Folgenden Code hat das Test-Projekt für die Lib</p>
<pre><code>// testrandom.cpp

#include &lt;iostream&gt;
#include &quot;random.h&quot;

using namespace std;

int main() {
    for( int i = 0; i &lt; 20; i++ ) {
        cout &lt;&lt; random( 0, 101 ) &lt;&lt; endl; // Zufallszahl zwischen 0 und 100
    }

    int size = 20;
    int* array = new int[size];

    for( int i = 0; i &lt; 20; i++ ) { // Array mit Werten belegen
        arr[i] = i*i;
    }

    for( int i = 0; i &lt; 20; i++ ) { // Zufälligen Wert aus Array ausgeben
        cout &lt;&lt; randOfArr( arr, size ) &lt;&lt; endl;
    }

    delete[] arr;
    return 0;
}
</code></pre>
<p>Beim komplilieren bekomme ich nun folgende Fehlermeldung( Dev-Cpp ):</p>
<p>[Linker error] undefined reference to 'int randOfArr&lt;int&gt;(int*, int)'</p>
<p>Allerdings kann es nicht an der lib selber liegen da er die andere random Funktion auch erkennt. Daraus schließe ich dass er Probleme mit dem template hat.<br />
Bitte kann mir jemand helfen.</p>
<p>Danke im voraus</p>
<p>Gruß andre111</p>
]]></description><link>https://www.c-plusplus.net/forum/topic/212819/gelöst-template-funktion-wird-vom-linker-nicht-erkannt</link><generator>RSS for Node</generator><lastBuildDate>Wed, 14 Oct 2026 16:00:40 GMT</lastBuildDate><atom:link href="https://www.c-plusplus.net/forum/topic/212819.rss" rel="self" type="application/rss+xml"/><pubDate>Sat, 10 May 2008 19:38:10 GMT</pubDate><ttl>60</ttl><item><title><![CDATA[Reply to [gelöst]template Funktion wird vom Linker nicht erkannt on Sat, 17 May 2008 09:08:29 GMT]]></title><description><![CDATA[<p>Hallo,</p>
<p>Ich hab mir eine kleine statische lib geschrieben, in der ich zwei Funktionen definiert habe. Eine die eine Pseudo-Zufallszahl zwischen 2 Werten zurückgibt und eine die einen Wert aus einem Array ( als template-Funktion ) zurückgibt.</p>
<p>Folgendes ist der Code dazu</p>
<pre><code>// random.h

int random( int min, int max ); // Pseudo-Zufallszahl erstellen

template&lt;class T&gt;
T randOfArr( T* arr, int size ); // Zufälliges Element aus Array auswählen
</code></pre>
<pre><code>// random.cpp

#include &lt;ctime&gt;
#include &lt;cstdlib&gt;
#include &quot;random.h&quot;

using namespace std;

bool doonce = true; // Damit srand nur einmal initialisiert wird

int random( int min, int max ) {
    if( doonce ) {
        srand( time(0) );
        doonce = false;
    }
    return (rand() % max + min);
}

template&lt;class T&gt;
T randOfArr( T* arr, int size ) {
    if( doonce ) {
        srand( time(0) );
        doonce = false;
    }
    return arr[rand()%size];
}
</code></pre>
<p>Dies lässt sich ohne Weiteres zu einer .lib kompilieren.<br />
In einem Dev-Cpp Projekt habe ich nun die header-Datei included und die lib dem<br />
Linker hinzugefügt.</p>
<p>Folgenden Code hat das Test-Projekt für die Lib</p>
<pre><code>// testrandom.cpp

#include &lt;iostream&gt;
#include &quot;random.h&quot;

using namespace std;

int main() {
    for( int i = 0; i &lt; 20; i++ ) {
        cout &lt;&lt; random( 0, 101 ) &lt;&lt; endl; // Zufallszahl zwischen 0 und 100
    }

    int size = 20;
    int* array = new int[size];

    for( int i = 0; i &lt; 20; i++ ) { // Array mit Werten belegen
        arr[i] = i*i;
    }

    for( int i = 0; i &lt; 20; i++ ) { // Zufälligen Wert aus Array ausgeben
        cout &lt;&lt; randOfArr( arr, size ) &lt;&lt; endl;
    }

    delete[] arr;
    return 0;
}
</code></pre>
<p>Beim komplilieren bekomme ich nun folgende Fehlermeldung( Dev-Cpp ):</p>
<p>[Linker error] undefined reference to 'int randOfArr&lt;int&gt;(int*, int)'</p>
<p>Allerdings kann es nicht an der lib selber liegen da er die andere random Funktion auch erkennt. Daraus schließe ich dass er Probleme mit dem template hat.<br />
Bitte kann mir jemand helfen.</p>
<p>Danke im voraus</p>
<p>Gruß andre111</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1506759</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1506759</guid><dc:creator><![CDATA[andre111]]></dc:creator><pubDate>Sat, 17 May 2008 09:08:29 GMT</pubDate></item><item><title><![CDATA[Reply to [gelöst]template Funktion wird vom Linker nicht erkannt on Sat, 10 May 2008 20:21:14 GMT]]></title><description><![CDATA[<p><a href="http://www.c-plusplus.net/forum/viewtopic-var-t-is-151578.html" rel="nofollow">http://www.c-plusplus.net/forum/viewtopic-var-t-is-151578.html</a></p>
<p>Nummer 5</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1506789</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1506789</guid><dc:creator><![CDATA[Michael E.]]></dc:creator><pubDate>Sat, 10 May 2008 20:21:14 GMT</pubDate></item><item><title><![CDATA[Reply to [gelöst]template Funktion wird vom Linker nicht erkannt on Sat, 10 May 2008 20:25:15 GMT]]></title><description><![CDATA[<p>gut, danke</p>
<p>andre111</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1506791</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1506791</guid><dc:creator><![CDATA[andre111]]></dc:creator><pubDate>Sat, 10 May 2008 20:25:15 GMT</pubDate></item><item><title><![CDATA[Reply to [gelöst]template Funktion wird vom Linker nicht erkannt on Sat, 10 May 2008 20:25:52 GMT]]></title><description><![CDATA[<p>Im cpp ein export vor die Definition:</p>
<pre><code class="language-cpp">export template&lt;class T&gt;
T randOfArr( T* arr, int size ) {...}
</code></pre>
<p>Wenns dann immer noch nicht geht, unterstützt Dein Compiler das Separationsmodell nicht, und Du musst die Definition in den Header packen.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1506792</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1506792</guid><dc:creator><![CDATA[Tachyon]]></dc:creator><pubDate>Sat, 10 May 2008 20:25:52 GMT</pubDate></item><item><title><![CDATA[Reply to [gelöst]template Funktion wird vom Linker nicht erkannt on Sat, 10 May 2008 20:34:30 GMT]]></title><description><![CDATA[<p>Tachyon schrieb:</p>
<blockquote>
<p>Im cpp ein export vor die Definition:</p>
<pre><code class="language-cpp">export template&lt;class T&gt;
T randOfArr( T* arr, int size ) {...}
</code></pre>
<p>Wenns dann immer noch nicht geht, unterstützt Dein Compiler das Separationsmodell nicht, und Du musst die Definition in den Header packen.</p>
</blockquote>
<p>Was sehr wahrscheinlich ist, ausser er hat Comeau..</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1506800</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1506800</guid><dc:creator><![CDATA[drakon]]></dc:creator><pubDate>Sat, 10 May 2008 20:34:30 GMT</pubDate></item><item><title><![CDATA[Reply to [gelöst]template Funktion wird vom Linker nicht erkannt on Sun, 11 May 2008 00:07:34 GMT]]></title><description><![CDATA[<p>ROFL<br />
Immer diese sinnvollen Vorschläge.<br />
Ein bisschen Realismus (-&gt; &quot;keiner&quot; arbeitet mit einem Compiler der &quot;export&quot; unterstützt) könnte manchmal schon nicht schaden.</p>
<p><a class="plugin-mentions-user plugin-mentions-a" href="https://www.c-plusplus.net/forum/uid/18207">@andre111</a>:</p>
<blockquote>
<pre><code class="language-cpp">bool doonce = true; // Damit srand nur einmal initialisiert wird
</code></pre>
</blockquote>
<p>Das ist ganz schlechter Stil. Pack das lieber in die Funktion, so:</p>
<pre><code class="language-cpp">int random( int min, int max ) {
    static bool doonce = true;
    if( doonce ) {
        srand( time(0) );
        doonce = false;
    }
    return (rand() % max + min);
}
</code></pre>
<p>Das ist zwar immer noch grausam, weil's mit mehreren Threads dann nicht so funktioniert wie erwartet, aber naja.</p>
<p>Tip: Verwende RNGs die aus einer eigenen Klasse bestehen, wie z.B. die Generatoren von Boost.Random. Dann ergibt sich das Problem mit dem Seeden nicht in der Form.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1506844</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1506844</guid><dc:creator><![CDATA[hustbaer]]></dc:creator><pubDate>Sun, 11 May 2008 00:07:34 GMT</pubDate></item><item><title><![CDATA[Reply to [gelöst]template Funktion wird vom Linker nicht erkannt on Sun, 11 May 2008 00:07:55 GMT]]></title><description><![CDATA[<pre><code class="language-cpp">int random( int min, int max ) {
   static const bool doonce = ( srand( time(0) ), true );
   return rand() % max + min;
}
</code></pre>
]]></description><link>https://www.c-plusplus.net/forum/post/1506846</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1506846</guid><dc:creator><![CDATA[camper]]></dc:creator><pubDate>Sun, 11 May 2008 00:07:55 GMT</pubDate></item><item><title><![CDATA[Reply to [gelöst]template Funktion wird vom Linker nicht erkannt on Sun, 11 May 2008 22:04:00 GMT]]></title><description><![CDATA[<p>camper schrieb:</p>
<blockquote>
<pre><code class="language-cpp">int random( int min, int max ) {
   static const bool doonce = ( srand( time(0) ), true );
   return rand() % max + min;
}
</code></pre>
</blockquote>
<p>ROFL<br />
Immer diese sinnvollen Vorschläge.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1507228</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1507228</guid><dc:creator><![CDATA[hustbär]]></dc:creator><pubDate>Sun, 11 May 2008 22:04:00 GMT</pubDate></item></channel></rss>