templates and linking problems

D

david

Hello, I decided to play a bit with templates and classes, but
discovered some problems.
So, I have 3 files: LinkedList.h LinkedList.cpp and test.cpp (by the
way it is not finished fully)

LinkedList.h

#ifndef LinkedList_byDavid
#define LinkedList_byDavid

template <class lType>
class LinkedList {
public:
LinkedList() : lSize(0) {};
void add(lType item);
lType get(int num) const;
int Size() const;
//~LinkedList();

private:
typedef struct child {
lType *value;
child *next;
} child;
child *root;
int lSize;
};

#endif

LinkedList.cpp

#include <cstring>
#include "LinkedList.h"

template <class lType>
int LinkedList<lType>::Size() const {
return lSize;
}

template <class lType>
void LinkedList<lType>::add(lType item) {
child *newChild = new child;
newChild->value = new lType;
newChild->value = item;
newChild->next = root;
root = newChild;
lSize++;
}

and test.cpp:

#include <iostream>
#include <cstring>
#include "LinkedList.h"

using namespace std;

int main (int argc, char const *argv[])
{
cout << "Bandymas" << endl;
LinkedList<int> as;
LinkedList<int> *tu = new LinkedList<int>();
as.add(5);
as.add(5);
as.add(5);
as.add(5);
as.add(5);
int kazkas = as.Size();
cout << "dydis: " << kazkas << endl;
cout << (*tu).Size() << endl;

return 0;
}

I use this to compile and link:
g++ -Wall -ansi -pedantic -c LinkedList.cpp
g++ -Wall -ansi -pedantic -c test.cpp
g++ -o test test.o linkedlist.o

And getting:
Undefined symbols:
"LinkedList<int>::add(int)", referenced from:
_main in test.o
_main in test.o
_main in test.o
_main in test.o
_main in test.o
"LinkedList<int>::Size() const", referenced from:
_main in test.o
_main in test.o
ld: symbol(s) not found
collect2: ld returned 1 exit status

on gcc version 4.0.1 (Apple Inc. build 5465)

There could be two problems:
a) declarations does not match, but as I look it looks that everything
is okay.
b) something wrong with LinkedList.cpp, maybe I missed something.

Any tips what I have missed here?
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top