Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > templates and linking problems

Reply
Thread Tools

templates and linking problems

 
 
david
Guest
Posts: n/a
 
      04-15-2008
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?


 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Linking code with templates and template libraries bav.272304@gmail.com C++ 3 05-07-2007 02:44 PM
how to Specializations of function Templates or Overloading Function templates with Templates ? recover C++ 2 07-25-2006 02:55 AM
Templates templates templates JKop C++ 3 07-21-2004 11:44 AM
linking errors when using templates murali C++ 1 09-02-2003 06:11 AM
linking templates problem Tobias Langner C++ 1 07-30-2003 08:28 PM



Advertisments