0

Possible Duplicate:
Why can templates only be implemented in the header file?

Main class

   int main() {
      initCarList();
   }

   void initCarList() {
        List<Car> carList;
        Car c1 = Car("Toyota", "Bettle", 5);
        carList.add(c1);
        Car c2 = Car("Mercedes", "Bettle", 7);
        carList.add(c2);
        Car c3 = Car("FireTruck", "Large Van", 20);
        carList.add(c3);
        Car c4 = Car("Puma", "Saloon Car", 10);
        carList.add(c4);
    }

List class

#include "List.h"
#include <iostream>

using namespace std;

template <typename ItemType>
class List {
private:
    ItemType itemList[10];
    int size;
public: 
    List();
    void add(ItemType);
    void del(int index);
    bool isEmpty();
    ItemType get(int);
    int length();
};

template<typename ItemType>
List<ItemType>::List() {
    size = 0;
}

template<typename ItemType>
void List<ItemType>::add(ItemType item) {
    if(size < MAX_SIZE) {
        itemList[size] = item;
        size++; 
    } else {
        cout << typename << " list is full.\n";
    }
}

I got errors like these

Error 3 error LNK2019: unresolved external symbol "public: void __thiscall List::add(class Car)" (?add@?$List@VCar@@@@QAEXVCar@@@Z) referenced in function "void __cdecl initCarList(void)" (?initCarList@@YAXXZ) C:\Users\USER\Desktop\New folder\DSA_Assignment\main.obj DSA_Assignment

Did I do anything wrongly in my code? NEED HELP THANKS!

Community
  • 1
  • 1
unknown
  • 57
  • 2
  • 8
  • 6
    Template definitions need to be in header. See [here](http://stackoverflow.com/questions/495021/why-can-templates-only-be-implemented-in-the-header-file). Also, accept some answers. – Angew is no longer proud of SO Jan 08 '13 at 11:34
  • 1
    The error message is about `initWorkerList()`, not `initCarList()`. – Barmar Jan 08 '13 at 11:36
  • It says `initWorkerList()` cannot see the code for `add()` – Neel Basu Jan 08 '13 at 11:46
  • 1
    `out << typename << " list is full.\n"` is wrong . typename cannot be there – Neel Basu Jan 08 '13 at 11:47
  • 1
    The code posted above doesn't fully relate to the error in question (although, yes, the `cout << typename` bit is definitely wrong.) – Component 10 Jan 08 '13 at 11:57
  • As an aside observation, C++ programmers always seem to be taught how to create linked lists when in practice, no-one has really done this in the real world for a decade or so thanks to STL. On the other hand I don't ever recall being taught how to interpret compiler error messages and yet I do this many times a day! – Component 10 Jan 08 '13 at 12:02

2 Answers2

0

There is a syntax error (cout << typename ) in your code. I don't know how you got the linker error. May be its not being compiled at all.

otherwise its okay http://ideone.com/PGWGZu

Neel Basu
  • 12,638
  • 12
  • 82
  • 146
  • Thanks. I don't know if typename works but I have compiled it and it gave me that only the linker error. I am also confused. – unknown Jan 08 '13 at 11:59
0

Clearly you did as it doesn't work! Flippancy aside, let's take a look at the error message bit by bit:

Error 3 error LNK2019: unresolved external symbol

So this is a linkage error. The linker is trying to put together the units that were individually compiled together, but in this case it can't find an external symbol - usually a function or variable name.

"public: void __thiscall List::add(class Worker)" (?add@?$List@VWorker@@@@QAEXVWorker@@@Z)

This is the full signature of the function that you're missing. It's name manged unfortunately but with your context knowledge of the code that you're writing, you should be able to tell that it's:

void List::add(Worker)

The next bit ...

referenced in function "void __cdecl initWorkerList(void)" (?initWorkerList@@YAXXZ) C:\Users\USER\Desktop\New folder\DSA_Assignment\main.obj DSA_Assignment

... is telling you where the problem is happening, i.e where in the code it's trying to link, there is a reference to the missing function. Again, after demangling it's in:

void initWorkerList()

As you can see, with a bit of graft, you can determine exactly what you've done wrong here. Hope this helps.

Component 10
  • 10,247
  • 7
  • 47
  • 64
  • hi, thanks for noting. I realised I have copied the wrong one. And I have edited out the workers to cars. – unknown Jan 08 '13 at 11:57