Note that there are some explanatory texts on larger screens.

plurals
  1. POHow exactly should template classes be defined and implemented properly in C++?
    primarykey
    data
    text
    <p>I recently decided to do some personal C++ practice by writing simple structures to ease into C++ development. My first attempt was to write a simple LinkedList but define it as a template class so that it could handle multiple data types. While doing so I did the standard practice of defining the class in a header file and implementing it in a cpp file and then I used make to build it (with <code>686-apple-darwin11-llvm-g++-4.2 (GCC) 4.2.1</code>). As far as building the C++ files (which included a <code>Node.cpp</code>, <code>LinkedList.cpp</code> and <code>LinkedListIterator.cpp</code> everything worked fine with no complaints. I then introduce <code>main.cpp</code> which contained the main method and tried to create a <code>LinkedList&lt;string&gt;</code> (which worked) and then tried to call it's length function which I both defined and implemented:</p> <pre><code>// Other code here LinkedList&lt;string&gt; *list = new LinkedList&lt;string&gt;(); cout &lt;&lt; "Initial length: " &lt;&lt; list-&gt;length() &lt;&lt; endl; delete list; list = NULL; // I had defined NULL to be 0, pre 4.6 compiler so nullptr wasn't available // End the main function </code></pre> <p>This threw an error that no <code>length()</code> function was defined for the templated <code>LinkedList&lt;string&gt;</code> class and so I began my research where I learned that template classes cannot be defined in the standard header/implementation format like standard C++ classes but none of these responses (mostly here) addressed how to properly handle template classes. So to finish off my personal exercise I dropped the headers and just defined and implemented the classes in the cpp file. Everything worked fine there.</p> <p>There were some "work arounds" that supposedly made it possible to separate implementation from definition such as including the cpp file at the end of the header file (which failed to work for me) or, provided you only want your class to work with specific types, to declare static template versions of your class at the base of your header file like:</p> <pre><code>// Define a template class template LinkedList&lt;string&gt;; template LinkedList&lt;int&gt;; // etc... </code></pre> <p>Which I didn't try (it may have worked, and as I was only doing a personal exercise it was perfectly acceptable) because this just feels wrong. Why make a template class if you lock it into certain types? I realize you could easily lock it into to all the major types you need giving you one definition for several data types but it just doesn't sit right with me to do things this way.</p> <p><strong>TL;DR</strong> Ultimately my question is, what is the proper way to define a template class and implement a template class in some kind of application/library other people would be using?</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload