<?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[Problem mit templates]]></title><description><![CDATA[<p>hallo,</p>
<p>ich habe mich gerade an meinem ersten C++ Template versucht, mit einer Struktur hab ichs ohnen Probleme hingebracht, nur sobald ich von meinem Klassen Template ein Objekt erzeugen will gibts einen Linker Error (Zeiger auf Klassen Template funktioniert....)</p>
<p>Ich habe keine Ahnung was nicht passt, hier der Code :</p>
<p>dictionary.h</p>
<pre><code class="language-cpp">#ifndef documentH // um header-files nicht mehrfach zu inkludieren
#define documentH // um header-files nicht mehrfach zu inkludieren

#ifdef HAS_EXPORT
  #define EXPORT export
#else
  #define EXPORT
#endif

#include &lt;string&gt;
#include &lt;vector&gt;

EXPORT template&lt;typename T&gt;
struct key_value {
  std::string key;
  T value;
};

EXPORT template&lt;typename T&gt;
class Dictionary{
 public:
  Dictionary();
  key_value&lt;T&gt; get_pair(int pos);
  void add_pair(std::string key, T value);
  void add_value(std::string key){}

 private:
  std::vector&lt;key_value&lt;T&gt; &gt; *keys_values;
};

#ifdef NEED_TEMPLATE_DEFINITIONS
  #include &quot;dictionary.cpp&quot;
#endif
#endif // um header-files nicht mehrfach zu inkludieren
</code></pre>
<p>dictionary.cpp</p>
<pre><code class="language-cpp">#include &quot;dictionary.h&quot;

#include &lt;iostream&gt;

EXPORT template&lt;typename T&gt;
Dictionary&lt;T&gt;::Dictionary(){
  key_value&lt;T&gt; new_pair;

  keys_values = new std::vector&lt;key_value&lt;T&gt; &gt;();
}

EXPORT template&lt;typename T&gt;
key_value&lt;T&gt; Dictionary&lt;T&gt;::get_pair(int pos){
  // Returns the struct key_value at $pos,
  // throws std::out_of_range when $pos is invalid 
  return keys_values-&gt;at(pos);
}

EXPORT template&lt;typename T&gt;
void Dictionary&lt;T&gt;::add_pair(std::string key, T value){
  // adds a struct with $key and $value to the dictionary
  key_value&lt;T&gt; pair;

  pair.key = key;
  pair.value = value;

  return keys_values-&gt;push_back(pair);
}
</code></pre>
<p>und der Code zum &quot;testen&quot; (CPPUNIT)</p>
<pre><code class="language-cpp">....
#include &quot;../../core/dictionary.h&quot;

class DictionaryTest : public CPPUNIT_NS :: TestFixture{ 
  CPPUNIT_TEST_SUITE (DictionaryTest);
  CPPUNIT_TEST (testDictionaryConstructor);
  CPPUNIT_TEST (testDictionaryAdd_pair);
  CPPUNIT_TEST_SUITE_END ();

 public: 
  Dictionary&lt;std::string &gt; *dict1;

  void setUp(){
    dict1 = new Dictionary&lt;std::string &gt;(); //-&gt;fuehrt zu dem Linkerfehler
  }
  ....
};
</code></pre>
<p>und zu guter letzt noch die Compilermeldung (gcc v4.3)</p>
<pre><code>g++ -Wall -o run_tests   run_tests.o test_document.o test_dictionary.o dictionary.o document.o sqlite3_backend.o test_sqlite3_backend.o `pkg-config sqlite3  --cflags --libs`  `pkg-config cppunit  --cflags --libs`
test_dictionary.o: In function `DictionaryTest::setUp()':
test_dictionary.cpp:(.text._ZN14DictionaryTest5setUpEv[DictionaryTest::setUp()]+0x22): undefined reference to `Dictionary&lt;std::basic_string&lt;char, std::char_traits&lt;char&gt;, std::allocator&lt;char&gt; &gt; &gt;::Dictionary()'
collect2: ld returned 1 exit status
make: *** [test] Error 1
</code></pre>
<p>hoffe, dass mir jemand helfen kann</p>
<p>mfg</p>
]]></description><link>https://www.c-plusplus.net/forum/topic/223221/problem-mit-templates</link><generator>RSS for Node</generator><lastBuildDate>Sat, 03 Oct 2026 23:47:13 GMT</lastBuildDate><atom:link href="https://www.c-plusplus.net/forum/topic/223221.rss" rel="self" type="application/rss+xml"/><pubDate>Tue, 23 Sep 2008 19:09:34 GMT</pubDate><ttl>60</ttl><item><title><![CDATA[Reply to Problem mit templates on Tue, 23 Sep 2008 19:09:34 GMT]]></title><description><![CDATA[<p>hallo,</p>
<p>ich habe mich gerade an meinem ersten C++ Template versucht, mit einer Struktur hab ichs ohnen Probleme hingebracht, nur sobald ich von meinem Klassen Template ein Objekt erzeugen will gibts einen Linker Error (Zeiger auf Klassen Template funktioniert....)</p>
<p>Ich habe keine Ahnung was nicht passt, hier der Code :</p>
<p>dictionary.h</p>
<pre><code class="language-cpp">#ifndef documentH // um header-files nicht mehrfach zu inkludieren
#define documentH // um header-files nicht mehrfach zu inkludieren

#ifdef HAS_EXPORT
  #define EXPORT export
#else
  #define EXPORT
#endif

#include &lt;string&gt;
#include &lt;vector&gt;

EXPORT template&lt;typename T&gt;
struct key_value {
  std::string key;
  T value;
};

EXPORT template&lt;typename T&gt;
class Dictionary{
 public:
  Dictionary();
  key_value&lt;T&gt; get_pair(int pos);
  void add_pair(std::string key, T value);
  void add_value(std::string key){}

 private:
  std::vector&lt;key_value&lt;T&gt; &gt; *keys_values;
};

#ifdef NEED_TEMPLATE_DEFINITIONS
  #include &quot;dictionary.cpp&quot;
#endif
#endif // um header-files nicht mehrfach zu inkludieren
</code></pre>
<p>dictionary.cpp</p>
<pre><code class="language-cpp">#include &quot;dictionary.h&quot;

#include &lt;iostream&gt;

EXPORT template&lt;typename T&gt;
Dictionary&lt;T&gt;::Dictionary(){
  key_value&lt;T&gt; new_pair;

  keys_values = new std::vector&lt;key_value&lt;T&gt; &gt;();
}

EXPORT template&lt;typename T&gt;
key_value&lt;T&gt; Dictionary&lt;T&gt;::get_pair(int pos){
  // Returns the struct key_value at $pos,
  // throws std::out_of_range when $pos is invalid 
  return keys_values-&gt;at(pos);
}

EXPORT template&lt;typename T&gt;
void Dictionary&lt;T&gt;::add_pair(std::string key, T value){
  // adds a struct with $key and $value to the dictionary
  key_value&lt;T&gt; pair;

  pair.key = key;
  pair.value = value;

  return keys_values-&gt;push_back(pair);
}
</code></pre>
<p>und der Code zum &quot;testen&quot; (CPPUNIT)</p>
<pre><code class="language-cpp">....
#include &quot;../../core/dictionary.h&quot;

class DictionaryTest : public CPPUNIT_NS :: TestFixture{ 
  CPPUNIT_TEST_SUITE (DictionaryTest);
  CPPUNIT_TEST (testDictionaryConstructor);
  CPPUNIT_TEST (testDictionaryAdd_pair);
  CPPUNIT_TEST_SUITE_END ();

 public: 
  Dictionary&lt;std::string &gt; *dict1;

  void setUp(){
    dict1 = new Dictionary&lt;std::string &gt;(); //-&gt;fuehrt zu dem Linkerfehler
  }
  ....
};
</code></pre>
<p>und zu guter letzt noch die Compilermeldung (gcc v4.3)</p>
<pre><code>g++ -Wall -o run_tests   run_tests.o test_document.o test_dictionary.o dictionary.o document.o sqlite3_backend.o test_sqlite3_backend.o `pkg-config sqlite3  --cflags --libs`  `pkg-config cppunit  --cflags --libs`
test_dictionary.o: In function `DictionaryTest::setUp()':
test_dictionary.cpp:(.text._ZN14DictionaryTest5setUpEv[DictionaryTest::setUp()]+0x22): undefined reference to `Dictionary&lt;std::basic_string&lt;char, std::char_traits&lt;char&gt;, std::allocator&lt;char&gt; &gt; &gt;::Dictionary()'
collect2: ld returned 1 exit status
make: *** [test] Error 1
</code></pre>
<p>hoffe, dass mir jemand helfen kann</p>
<p>mfg</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1586791</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1586791</guid><dc:creator><![CDATA[d3f3nd3r]]></dc:creator><pubDate>Tue, 23 Sep 2008 19:09:34 GMT</pubDate></item><item><title><![CDATA[Reply to Problem mit templates on Wed, 24 Sep 2008 00:29:35 GMT]]></title><description><![CDATA[<p>Code scheint in Ordnung.</p>
<p>Versuch es einfach ohne die #ifdef-Orgie, nur Inclusionsmodel statt diesem Mix aus Inclusion und Separation; möglicherweise löst sich dann dein Problem von selbst.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1586917</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1586917</guid><dc:creator><![CDATA[Kritiker]]></dc:creator><pubDate>Wed, 24 Sep 2008 00:29:35 GMT</pubDate></item><item><title><![CDATA[Reply to Problem mit templates on Wed, 24 Sep 2008 04:55:09 GMT]]></title><description><![CDATA[<p>Templates müssen zur Instanzierung vollständig bekannt sein. Deshalb müssen die Templates aus dictionary.cpp in die dictionary.h, da ich zunächst mal davon ausgehe, dass Dein Compiler HAS_EXPORT nicht definiert.</p>
<p>EDIT:<br />
Ifdef unten nicht gesehen. Ist denn dieses Define gesetzt, was die cpp includiert?</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1586933</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1586933</guid><dc:creator><![CDATA[LordJaxom]]></dc:creator><pubDate>Wed, 24 Sep 2008 04:55:09 GMT</pubDate></item><item><title><![CDATA[Reply to Problem mit templates on Wed, 24 Sep 2008 07:53:18 GMT]]></title><description><![CDATA[<p>also ich hab jetzt die Methoden Definition in die Headerdatei gegeben, funktioniert tadellos.</p>
<p>Wie erkenne ich ob mein compiler die HAS_EXPORT definiert?</p>
<p>Was meinst du mit &quot;Inclusionsmodel&quot; ?</p>
<p>mfg</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1587014</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1587014</guid><dc:creator><![CDATA[d3f3nd3r]]></dc:creator><pubDate>Wed, 24 Sep 2008 07:53:18 GMT</pubDate></item><item><title><![CDATA[Reply to Problem mit templates on Wed, 24 Sep 2008 08:01:06 GMT]]></title><description><![CDATA[<p>d3f3nd3r schrieb:</p>
<blockquote>
<p>Wie erkenne ich ob mein compiler die HAS_EXPORT definiert?</p>
<p>Was meinst du mit &quot;Inclusionsmodel&quot; ?</p>
</blockquote>
<p>Wenn du das alles nicht weißt, solltest du die ganzen #ifdef-Schlachten einfach weglassen. Offenbar hast du das von irgendwem abgeschrieben der ganz gewitzt sein wollte und die export-Funktionalität nutzen wollte, wenn es sie denn gibt (es gibt ein oder zwei Compiler die das überhaupt können).</p>
<p>Machs also einfach so:</p>
<pre><code class="language-cpp">#ifndef documentH // um header-files nicht mehrfach zu inkludieren
#define documentH // um header-files nicht mehrfach zu inkludieren

#include &lt;string&gt;
#include &lt;vector&gt;

template&lt;typename T&gt;
struct key_value {
  std::string key;
  T value;
};

template&lt;typename T&gt;
class Dictionary{
 public:
  Dictionary();
  key_value&lt;T&gt; get_pair(int pos);
  void add_pair(std::string key, T value);
  void add_value(std::string key){}

 private:
  std::vector&lt;key_value&lt;T&gt; &gt; *keys_values;
};

#include &quot;dictionary.cpp&quot;
#endif // um header-files nicht mehrfach zu inkludieren
</code></pre>
<p>und die .cpp wie gehabt</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1587025</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1587025</guid><dc:creator><![CDATA[pumuckl]]></dc:creator><pubDate>Wed, 24 Sep 2008 08:01:06 GMT</pubDate></item><item><title><![CDATA[Reply to Problem mit templates on Wed, 24 Sep 2008 08:58:30 GMT]]></title><description><![CDATA[<p>jup, hatte ich aus c++ in a nutshell <img
      src="https://www.c-plusplus.net/forum/plugins/nodebb-plugin-emoji/emoji/emoji-one/1f609.png?v=ab1pehoraso"
      class="not-responsive emoji emoji-emoji-one emoji--winking_face"
      title=";-)"
      alt="😉"
    /></p>
<p>aber mit dem include passiert folgendes :</p>
<pre><code>g++ -Wall -g -c src/core/dictionary.cpp 
src/core/dictionary.cpp:9: error: redefinition of ‘Dictionary&lt;T&gt;::Dictionary()’
src/core/dictionary.cpp:9: error: ‘Dictionary&lt;T&gt;::Dictionary()’ previously declared here
src/core/dictionary.cpp:15: error: redefinition of ‘key_value&lt;T&gt; Dictionary&lt;T&gt;::get_pair(int)’
src/core/dictionary.cpp:15: error: ‘key_value&lt;T&gt; Dictionary&lt;T&gt;::get_pair(int)’ previously declared here
src/core/dictionary.cpp:23: error: redefinition of ‘void Dictionary&lt;T&gt;::add_pair(std::string, T)’
src/core/dictionary.cpp:23: error: ‘void Dictionary&lt;T&gt;::add_pair(std::string, T)’ previously declared here
make: *** [dictionary.o] Fehler 1
</code></pre>
<p>die Quelltexte :<br />
dictionary.h</p>
<pre><code class="language-cpp">#ifndef dictionary_H // header file
#define dictionary_H // header file

/*
 $Id: dictionary.h 18 2008-09-24 08:18:48Z d3f3nd3r@bytegeist.org $
 */

#include &lt;string&gt;
#include &lt;vector&gt;

template&lt;typename T&gt;
struct key_value {
  std::string key;
  T value;
};

template&lt;typename T&gt;
class Dictionary{
 public:
  Dictionary();
  key_value&lt;T&gt; get_pair(int pos);
  void add_pair(std::string key, T value);
  void add_value(std::string key){}

 private:
  std::vector&lt;key_value&lt;T&gt; &gt; *keys_values;
};

#include &quot;dictionary.cpp&quot; 

#endif // header file
</code></pre>
<p>dictionary.cpp</p>
<pre><code class="language-cpp">/*
 $Id: dictionary.cpp 18 2008-09-24 08:18:48Z d3f3nd3r@bytegeist.org $
 */ 

#include &quot;dictionary.h&quot;

template&lt;typename T&gt;
Dictionary&lt;T&gt;::Dictionary(){
  key_value&lt;T&gt; new_pair;
  keys_values = new std::vector&lt;key_value&lt;T&gt; &gt;();
}

template&lt;typename T&gt;
key_value&lt;T&gt; Dictionary&lt;T&gt;::get_pair(int pos){
  // Returns the struct key_value at $pos,
  // throws std::out_of_range when $pos is invalid 

  return keys_values-&gt;at(pos);
}

template&lt;typename T&gt;
void Dictionary&lt;T&gt;::add_pair(std::string key, T value){
  // adds a struct with $key and $value to the dictionary

  key_value&lt;T&gt; pair;

  pair.key = key;
  pair.value = value;
  keys_values-&gt;push_back(pair);
}
</code></pre>
]]></description><link>https://www.c-plusplus.net/forum/post/1587077</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1587077</guid><dc:creator><![CDATA[d3f3nd3r]]></dc:creator><pubDate>Wed, 24 Sep 2008 08:58:30 GMT</pubDate></item><item><title><![CDATA[Reply to Problem mit templates on Wed, 24 Sep 2008 09:13:56 GMT]]></title><description><![CDATA[<p>die dictionary.cpp kannst du nicht gesondert kompilieren. Das ist ein Klassen-template das innerhalb der .cpp nicht instantiiert wird. Du bindest lediglich dort wo du Dictionary mit einem expliziten Typ benutzen willst die .h ein.<br />
Oft wird bei templates die Datei mit den Definitionen mit einer anderen Endung versehen, um von einer echten .cpp zu unterscheiden. Einige nennen sie z.B. .imp (für implementation) oder .tpl (für template) oder auch .def (für definition)<br />
Dass die Fehlermeldung kommt ist ja eigentlich klar, wenn du nachvollziehst was der compiler mit den includes anstellt:</p>
<pre><code>Dictionary.cpp Anfang
#include Dictionary.h
  Dictionary.h Anfang
  #ifdef includeguard...
  Dictionary Klassendefinition
  #include Dictionary.cpp
    Dictionary.cpp Anfang
    #include Dictionary.h
      Dictionary.h Anfang
      #ifdef includeguard... -&gt; sprung zum endif
      #endif
      Dictionary.h Ende
    Dictionary::Dictionary() {/*...*/} //erste Ctor-Definition
    Dictionary.cpp Ende
  #endif
  Dictionary.h Ende
Dictionary::Dictionary() {/*...*/} //zweite Ctor-Definition, Aua!
Dictionary.cpp Ende
</code></pre>
<p>in der template-definitionsdatei wird normalerweise der header nicht mit eingebunden, da der header ja andersrum die .tpl (oder .imp oder .def) einbindet.</p>
<p>Das ist übrigens das inclusion-modell: im header wird erst die klassendefinition mit methodendeklarationen gemacht, danach wird per #include die source-datei mit den methodendefinitionen eingebunden.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1587092</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1587092</guid><dc:creator><![CDATA[pumuckl]]></dc:creator><pubDate>Wed, 24 Sep 2008 09:13:56 GMT</pubDate></item><item><title><![CDATA[Reply to Problem mit templates on Wed, 24 Sep 2008 09:26:57 GMT]]></title><description><![CDATA[<p>Vielen Dank, jetzt verstehe ich, was der Compiler mit Templates macht!</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1587110</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1587110</guid><dc:creator><![CDATA[d3f3nd3r]]></dc:creator><pubDate>Wed, 24 Sep 2008 09:26:57 GMT</pubDate></item><item><title><![CDATA[Reply to Problem mit templates on Wed, 24 Sep 2008 09:41:33 GMT]]></title><description><![CDATA[<p>d3f3nd3r schrieb:</p>
<blockquote>
<p>Vielen Dank, jetzt verstehe ich, was der Compiler mit Templates macht!</p>
</blockquote>
<p>Zumindest was er mit dem Sourcecode und den includes macht <img
      src="https://www.c-plusplus.net/forum/plugins/nodebb-plugin-emoji/emoji/emoji-one/1f609.png?v=ab1pehoraso"
      class="not-responsive emoji emoji-emoji-one emoji--winking_face"
      title=";)"
      alt="😉"
    /></p>
]]></description><link>https://www.c-plusplus.net/forum/post/1587119</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1587119</guid><dc:creator><![CDATA[pumuckl]]></dc:creator><pubDate>Wed, 24 Sep 2008 09:41:33 GMT</pubDate></item><item><title><![CDATA[Reply to Problem mit templates on Wed, 24 Sep 2008 09:50:59 GMT]]></title><description><![CDATA[<p>exakt <img
      src="https://www.c-plusplus.net/forum/plugins/nodebb-plugin-emoji/emoji/emoji-one/1f609.png?v=ab1pehoraso"
      class="not-responsive emoji emoji-emoji-one emoji--winking_face"
      title=";-)"
      alt="😉"
    /></p>
]]></description><link>https://www.c-plusplus.net/forum/post/1587131</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1587131</guid><dc:creator><![CDATA[d3f3nd3r]]></dc:creator><pubDate>Wed, 24 Sep 2008 09:50:59 GMT</pubDate></item></channel></rss>