0

Ive been working on a linked list implementation. Previously i had it working for only char as the data type. I tried implementing generics/templates so i could use any data type.

I have 4 files in my project. Clist.h, Clist.cpp, Main.cpp and EmptyListException.h.

Previously my program would not run when i had my Clist.cpp seperate from my Main.cpp. I then copied the entire class too my Main.cpp and it runs without a problem.

Is there a way i can do this so my Clist.cpp can be separate from my main file (which is a test harness for the linked list).

I can post the code if it would help.

Coding Mash
  • 3,338
  • 5
  • 24
  • 45
George
  • 337
  • 2
  • 7
  • 16

1 Answers1

2

For templates to work, they can't be separated between a *.h file and *.cpp file. The compiler needs to see the implementation of the template to use it.

See this similar question: Splitting templated C++ classes into .hpp/.cpp files--is it possible?

Community
  • 1
  • 1
Ryan
  • 2,378
  • 1
  • 19
  • 29
  • So i technically don't need Clist.cpp if i add all the code from Clist.cpp to my Main.cpp – George Oct 13 '12 at 17:20
  • Yes, but it would probably be better to move the templated code from `Clist.cpp` to `Clist.h` to prevent your `Main.cpp` from getting cluttered. For example, if you see the implementation of the standard vector library, all the generic code is in one header file. – Ryan Oct 13 '12 at 17:39