Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: C++ Templates OOP
Hi,
 
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
 
Thanks.
 
Ivan
Posted 13-Oct-12 10:59am
Edited 13-Oct-12 11:48am
v2
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

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
#endif
 
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.
  Permalink  
Comments
blackhattrick at 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
#include "Nodo.h"
#include
using std::cout;
#include
#include
 
#define SUCCESS 0
#define ERROR -1
#define EMPTY_LIST -2
 
template
class Lista
{
public:
Lista();
~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);
 
private:
int inLenght;
Nodo *ptrPrimero;
Nodo *ptrUltimo;
Nodo *ptrObtenerNuevoNodo(const TIPONODO &);
};
 
template
Lista::Lista()
{
ptrPrimero = 0;
ptrUltimo = 0;
}
 
//Here goes all my member functions...
#endif
</pre>
 
This is Main.cpp Here the Linker says that member functions from the List class are already defined
<pre lang="c++">
#include
using namespace std;
 
#include
#include
#include <process.h>
 
#include
 
#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...
}
</pre>
 
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;
}gsThreadArgs;
 
#endif
</pre>
 
I hope this is clear enough.
 
Thanks in advance
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 3

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.
  Permalink  

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

  Print Answers RSS
0 Sergey Alexandrovich Kryukov 735
1 OriginalGriff 385
2 CPallini 240
3 PIEBALDconsult 150
4 Magic Wonder 131
0 OriginalGriff 5,835
1 Sergey Alexandrovich Kryukov 5,198
2 CPallini 4,740
3 George Jonsson 3,167
4 Gihan Liyanage 2,470


Advertise | Privacy | Mobile
Web02 | 2.8.140916.1 | Last Updated 15 Oct 2012
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100