Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: C++TemplatesOOP
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
Your Filters
Interested
Ignored
     
0 Sergey Alexandrovich Kryukov 462
1 sanket saxena 355
2 OriginalGriff 235
3 Abhinav S 233
4 CPallini 205
0 Sergey Alexandrovich Kryukov 8,784
1 OriginalGriff 5,025
2 Peter Leow 3,839
3 Maciej Los 3,535
4 Er. Puneet Goel 3,107


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