Click here to Skip to main content
15,908,768 members
Please Sign up or sign in to vote.
4.00/5 (1 vote)
See more:

I made a template class for a linked list. List.h has the class definition and List.cpp has all the function members for inserting a node, deleting them, etc.

When I compile my project (VS2010) I got the linker error "LNK1120 unresolved externals"

I found that due compiler issues, template classes have to be declared all in the header file.

I did this, plus I added #include List.h to my main.cpp file. I compiled it and it went OK.

But now, I want to create a new class or struct in a diferent header file newheader.h, and that struct has a List Object as a private member. So, at the begining of my new header file, I declare #include List.h and after that I declare this struct and then I compile my code, I got the error "LNK1169: one or more multiply defined symbols found"

This is obviously because I added #include List.h in my new header file and the linker sees that my List member functions are declared in main.cpp and newclass.h

I can't figure a solution for this. Any help would be apreciated


Updated 13-Oct-12 11:48am

It sounds like you have a header block define problem,although i cannot tell without
any source code. Is all the source code in List.h encapsulated with something like

#ifndef LIST_H
#define LIST_H
 //source code here

Using define macros like this will help ensure the compiler only builds the symbols of
your source code once even if you reference the header in more than one file, otherwise
I think it will attempt to build the template library each time.
Share this answer
blackhattrick 13-Oct-12 18:44pm    
blackhattrick - 46 mins ago
Hi GBSC thank you for your answer,

Yes I forgot to mention that I have that macro declared in each one of my header files(_LIST_, _NODE_), and that's why I'm confused about linker errors.

I didn't put my code because is rather long and some parts are in spanish, here is some part of my code that I consider relevant (I tried to put <pre> tags for a cleaner code but it does not work):

Lista.h //File List.h in my explanation of the problem
<pre lang="c++">
#ifndef _LISTA_
#define _LISTA_

#include <new>
#include "Nodo.h"
#include <iostream>
using std::cout;
#include <stdlib.h>
#include <time.h>

#define SUCCESS 0
#define ERROR -1
#define EMPTY_LIST -2

class Lista
void vdInsertarFrente(const TIPONODO &);
void vdInsertarFinal(const TIPONODO &);
int inBorrarFrente(void);
int inBorrarFinal(void);
bool blVacia();
void vdImprimir();
int vdShuffle(void);
int inGetLenght();
TIPONODO * inObtenerDatoNodo(int);

int inLenght;
Nodo *ptrPrimero;
Nodo *ptrUltimo;
Nodo *ptrObtenerNuevoNodo(const TIPONODO &);

ptrPrimero = 0;
ptrUltimo = 0;

//Here goes all my member functions...

This is Main.cpp Here the Linker says that member functions from the List class are already defined
<pre lang="c++">
using namespace std;

#include <process.h>


#include "Lista.h" //Header of Lista.h
#include "Persona.h"
#include "Thread.h"
#include "GSALG.h" //A class that uses a List Object as a member

int main(void)
Lista<Persona> lstHombres;
Lista<Persona> lstMujeres;

//More code...

And this is another header file GSALG.h (in my explanation is newheader.h) that declares a struct that uses a List object as a member. So I include Lista.h in this file

<pre lang="c++">
#ifndef _GSALG_
#define _GSALG_

#include "Lista.h";
#include "Persona.h";

typedef struct gsThreadArgs
Lista<Persona> lstHombres;
Lista<Persona> lstMujeres;


I hope this is clear enough.

Thanks in advance
This error is not because of your template class, but because of the global variable gsThreadArgs

This object is visible through multiple compilation units.

The one way you can solve this is by , declare it extern wherever you have included the GSALG.h

Most probably this will solve your problem.
Share this answer

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900