2

Does anyone have any insight in to how this error occurs? I made a LinkedList a template in C++ and in my main method I have this code:

List<int> list;
list.insert(1, 9);

And I am getting this error on the first line:

`List' is not a template

I am including this file:

template <typename T>
class List
{
public:
       List();
       List(const List& aList);
       ~List();

       bool isEmpty() const;
       int getLength() const;
       void insert(int index, const T& newItem);
       void remove(int index);
       void retrieve(int index, T& dataItem) const;

private:
        struct ListNode
        {
               T item;
               ListNode *next;
        };

        int size;
        ListNode *head;

        ListNode *find(int index) const;
};

Not posting the implementation file for spacial reasons but I am post individual functions if necessary.

I tried changing List(const List& aList); to List(const List<T>& aList); but that didn't really change anything. Templating syntax confuses me >.<

  • 4
    Compiles fine for me when I mock the functions with empty bodies. The error must be somewhere else. Also, don't put template code in .cpp files, the compiler needs to know the exact code that needs to be instantiated, so it needs to be in a header. – Xeo Apr 27 '11 at 04:12
  • 2
    It compiles fine for me (modulo the undefined functions at link time) using g++. – Paul J. Lucas Apr 27 '11 at 04:13
  • Do you have the implementation in a separate file from the header? – jonsca Apr 27 '11 at 04:14
  • @Xeo: What do you mean don't put template code in .cpp files? Is something like this bad practice? http://pastebin.com/9xV42eUS – Guelgosh Puanetham Apr 27 '11 at 04:15
  • 1
    It's not bad practice, it is wrong. [See this](http://stackoverflow.com/questions/495021/why-can-templates-only-be-implemented-in-the-header-file). – Xeo Apr 27 '11 at 04:17
  • It's nice to find out that my textbook is feeding me false information. – Guelgosh Puanetham Apr 27 '11 at 04:19
  • 4
    The implementation of the template class methods must be available to the compiler at the instantiation point (where you use the template). The C++98 standard defines the `export` keyword for permitting separate compilation of template code, but it is not supported by the major compilers and is deprecated in the upcoming C++0x standard. So you have to include the implementation of the template in the header. – Begemoth Apr 27 '11 at 04:20
  • @Guelgosh: Take one of [these](http://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list). :) – Xeo Apr 27 '11 at 04:22
  • @Xeo: Sadly I am using a required class text and doing one of the problems from the book. – Guelgosh Puanetham Apr 27 '11 at 04:27
  • `I made a LinkedList a template`. No you didn't. – Hans Passant Apr 27 '11 at 04:57
  • If your header implementation is in a separate file, include it at the bottom of your header. – Ternary May 22 '11 at 21:56
  • I think everybody is missing the point. Split declaration from implementation must give *linker* errors, not compiler errors. Could you please post the full error messages so we can investigate if this error is not due to any previous error? – j4x May 31 '11 at 19:11
  • @Xeo: No, it's bad practice. The compiler doesn't give a crap about headers/source files. It sees the same stuff. It's just a real nightmare to make linking work properly when you put template definitions in source files, because _conventionally_ we do not include such code in all TUs independently. So putting template definitions in headers **should** definitely be done, but it's not strictly an outright error not to. (And, notably, if you only use the template in one TU, you'll have no problems whatsoever!) – Lightness Races in Orbit Jun 01 '11 at 13:08

3 Answers3

1

In my projects I declare list like this to avoid the error

    using namespace std;
    #include <list>
  • 2
    Maybe you should read this [Why is “using namespace std;” considered bad practice?](https://stackoverflow.com/questions/1452721/why-is-using-namespace-std-considered-bad-practice) – anastaciu Jan 30 '20 at 16:10
  • Using the standard library `std::list` doesn't answer this question. – Blastfurnace Jan 30 '20 at 17:40
  • I stand corrected about bad practice -But I was answering the original question. it should be #include then in all uses std::list – Gary Walkey Jan 31 '20 at 12:55
1

Are you sure that your List template class is not in a different namespace?

Have you tried renaming your template class to something unique to ensure you are instantiating the class you think you are?

zennehoy
  • 6,405
  • 28
  • 55
  • 1
    This does not provide an answer, please comment instead – GGO Mar 14 '18 at 10:06
  • @GGO I beg to differ. It provides two possible explanations for what may be causing this error. I phrased them as questions on purpose (7 years ago) because it is not certain that either of these are in fact the reason for the OP's error. – zennehoy Mar 15 '18 at 19:49
  • Someone flag this answer like a low quality answer. I only moderated it. And it's right, today, your answer doesn't compliant : https://stackoverflow.com/help/how-to-answer Sorry, but only SO Admin can decide now – GGO Mar 16 '18 at 08:02
-2

Is it possible that you wrote the template code in the .cpp file?

I'm going of by this line:

Not posting the implementation file

Template code can only ever be in the .h file, there can be no "implementation file".
The reasons are as complicated, as they are silly :)

x10
  • 3,820
  • 1
  • 24
  • 32