Jump to content
  • Advertisement
Sign in to follow this  
Kevn

templates?

This topic is 3491 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I made a linked list class for a school project and now I am trying to turn it into a template class. I can't seem to do it correctly so can someone please let me know what I am doing wrong? .h file code: #ifndef LINKEDLIST_H #define LINKEDLIST_H struct Node; template <class Item> class LinkedList { public: LinkedList(void); ~LinkedList(void); void startList(Item item); void addItem(Item item); void deleteList(void); void printList(void); int getItem(void); void checkDuplicate(void); private: Node* head; Node* holder; int count; bool listStart; bool starting; }; typedef Node* nodePtr; struct Node { int component; nodePtr link; }; #include "linkedlist.h" #endif I am defining member functions in the implimentation file like this: template <class Item> LinkedList<Item>::LinkedList(void) { head = NULL; count = 0; listStart = false; starting = false; } template <class Item> LinkedList<Item>::~LinkedList(void) { } template <class Item> void LinkedList<Item>::startList(Item item) { listStart = true; head = new Node; head->component = item; head->link = NULL; } i'm getting errors like missing ';' before '<' for "template <class Item>" but that cant be because thats the first line of code besides #include <iostream> in the file. also things about and int being assumed and c++ not supporting default int and an illegal destructor.

Share this post


Link to post
Share on other sites
Advertisement
You need to take the #include out of the header file and put it at the top of the implementation (.cpp) file.

At the moment your header file is including itself, and your cpp file isn't including anything, so it doesn't have access to the template declaration.

Hope that helps.

Share this post


Link to post
Share on other sites
i thought placing the include at the top of the implementation was just for classes and not template classes. Aren't templates not really supose to have implementation files but the way to sort of allow one, which i heard is bad practice, is by placing an include at the bottom of the header file?

I first made the templete with no implementation file and just wrote the functions directly in the header. It all worked fine but the assignment says to include an implementation file.


edit - ahhhh that should be linkedlist.cpp at the bottom not .h .. thats what i get for copying and pasting

bleh.. still not working. i made the change you suggested and its giving me less errors but still getting errors?

here is the sample of the errors i get after that change..
error LNK2019: unresolved external symbol "public: void __thiscall LinkedList<int>::deleteList(void)" (?deleteList@?$LinkedList@H@@QAEXXZ) referenced in function _main

Share this post


Link to post
Share on other sites
OK. Well, yes - any code that uses the LinkedList class will need access to the method implementations.

You can't really include the cpp file in the header file, as your compiler will still try to compile it. When it can't find the LinkedList declaration it throws out the errors you mentioned.

If you really want to, you can change the .cpp file to an inline file (.inl) and include it in the header. But maybe your implementation file is only meant to demonstrate the LinkedList, and not actually implement it? That would seem logical to me, anyway.

[Edit] Just read your edit. Are you implementing the deleteList() method?

Share this post


Link to post
Share on other sites
i fixed it, changed #include "linkedlist.cpp" at the bottom of the header to #include "linkedlist.template"

nothing about the assignments my teacher creates is logical..

i'm pretty sure the file is ment to implement it.. we need 3 files. 1 with a main where i input numbers and stuff, the header, and the implementation

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!