473,385 Members | 1,553 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

templates and linking problems

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<intas;
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?
Jun 27 '08 #1
1 1154
david wrote:
[..]

Any tips what I have missed here?
You missed part of the FAQ that explains link errors with templates.

V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
Jun 27 '08 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: murali | last post by:
Hi, I get tollowing linking errors Main.cpp:30 undefined reference to Matrix<int>::Matrix(int,int) whats that '' and what might be the problem ?? line 30 of main:...
9
by: Fred H | last post by:
I'm currently trying to write a function template that can fill a variable of arbitrary type with 'random' stuff, but I can't seem to get my function template working. In my .h file I've...
1
by: Rebecca Hoffmann | last post by:
Hi, I have a serious problem while compiling a small project (a part of the Modular Flow Scheduling Middleware: ex1): There are 3 linker errors, all from symbols that point to templates: --...
7
by: Vyacheslav Lanovets | last post by:
Hello, All! One of our target platforms has only 32 MB of virtual memory (Windows CE), so we decided to explicitly load some of our dlls. But the classes created inside such dlls are created...
14
by: aaragon | last post by:
Hi everyone, I've been writing some code and so far I have all the code written in the .h files in order to avoid the linker errors. I'm using templates. I wanted to move the implementations to...
3
by: bav.272304 | last post by:
I have a code which uses external library with templates. The separate compiling of project files gets definition of the same symbols in different object files, so linking fails. Actually,...
0
by: xieml2007 | last post by:
Dear Madam or Sir, I encountered one problem which is quite similiar to the discussions launched at the web site: http://www.thescripts.com/forum/thread280324.html
5
mythescriptid
by: mythescriptid | last post by:
Hi All, I know templates and STL might have been discussed many a times here, I'm bit of newbie as far as templates are concerned and was itching to test my new found linking of templates. I would...
1
by: Rahul | last post by:
Hi Everyone, I was looking at the link, http://www.comeaucomputing.com/4.0/docs/userman/export.html and it looks like the basic purpose of exporting templates is make sure that they are...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.