Linker Error Undefined Reference To Templates
here for a quick overview of the site Help Center Detailed answers to undefined reference to template member function any questions you might have Meta Discuss the workings and c++ undefined reference to template class method policies of this site About Us Learn more about Stack Overflow the company Business Learn undefined reference to template class constructor more about hiring developers or posting ads with us Stack Overflow Questions Jobs Documentation Tags Users Badges Ask Question x Dismiss Join the Stack Overflow Community
C++ Template Function Undefined Reference
Stack Overflow is a community of 6.2 million programmers, just like you, helping each other. Join them; it only takes a minute: Sign up Template issue causes linker error (C++) up vote 19 down vote favorite 6 I have very little idea what's going in regards to C++ templates, but I'm extern template trying to implement a function that searches a vector for an element satisfying a given property (in this case, searching for one with the name given). My declaration in my .h file is as follows: template T* find_name(std::vector v, std::string name); When I compile, I get this linker error when I call the function: Error 1 error LNK2019: unresolved external symbol "class Item * __cdecl find_name(class std::vector >,class std::basic_string,class std::allocator >)" (??$find_name@VItem@@@@YAPAVItem@@V?$vector@PAVItem@@V?$allocator@PAVItem@@@std@@@std@@V?$basic_string@DU?$char_traits@D@std@@V?$allocator@D@2@@2@@Z) referenced in function "public: class Item * __thiscall Place::get_item(class std::basic_string,class std::allocator >)" (?get_item@Place@@QAEPAVItem@@V?$basic_string@DU?$char_traits@D@std@@V?$allocator@D@2@@std@@@Z) place.obj Program2 Again, I'm new to templates so I don't know what's going. All instances I've found of LNK2019 through Google have been about not using the correct libraries, but since this is my own function I don't see why this would be happening. Also, a related question: Is there a way to make a template
here for a quick overview of the site Help Center Detailed answers template class implementation in cpp file to any questions you might have Meta Discuss the workings
C++ Template Instantiation
and policies of this site About Us Learn more about Stack Overflow the company Business
Template Class C++
Learn more about hiring developers or posting ads with us Stack Overflow Questions Jobs Documentation Tags Users Badges Ask Question x Dismiss Join the Stack http://stackoverflow.com/questions/1639797/template-issue-causes-linker-error-c Overflow Community Stack Overflow is a community of 6.2 million programmers, just like you, helping each other. Join them; it only takes a minute: Sign up “Undefined reference to” template class constructor [duplicate] up vote 73 down vote favorite 66 This question already has an answer here: Why can templates http://stackoverflow.com/questions/8752837/undefined-reference-to-template-class-constructor only be implemented in the header file? 12 answers I have no idea why this is happenning, since I think I have everything properly declared and defined. I have the following program, designed with templates. It's a simple implementation of a queue, with the member functions "add", "substract" and "print". I have defined the node for the queue in the fine "nodo_colaypila.h": #ifndef NODO_COLAYPILA_H #define NODO_COLAYPILA_H #include template class cola; template class nodo_colaypila { T elem; nodo_colaypila* sig; friend class cola; public: nodo_colaypila(T, nodo_colaypila*); }; Then the implementation in "nodo_colaypila.cpp" #include "nodo_colaypila.h" #include template nodo_colaypila::nodo_colaypila(T a, nodo_colaypila* siguiente = NULL) { elem = a; sig = siguiente;//ctor } Afterwards, the definition and declaration of the queue template class and its functions: "cola.h": #ifndef COLA_H #define COLA_H #include "nodo_colaypila.h" template class cola { nodo_colaypila* ult, pri; public: cola(); void anade(
1 of 1 New Topic/Question Reply 4 Replies - 9716 Views - Last Post: 12 August 2010 - 03:45 PM Rate Topic: #1 Guest_zeroth* Reputation: g++ undefined reference with class template Posted 12 August 2010 - 12:34 PM but if I remove all http://www.dreamincode.net/forums/topic/185707-g-undefined-reference-with-class-template/ the template stuff, it works. whats going on here? list.cpp #include "list.hh" template char List::Get( void ) const { return 'a'; } list.hh template class List { public: char Get( void ) const; }; try.cpp #include #include "list.hh" int main ( void ) { List list; printf("%c\n", list.Get()); return 0; } g++ list.cpp try.cpp /tmp/cclUhOCm.o: In function `main': try.cpp:(.text+0x11): undefined reference to `List::Get() const' collect2: ld returned 1 exit status Is This undefined reference A Good Question/Topic? 0 Back to top MultiQuote Quote + Reply Replies To: g++ undefined reference with class template #2 sarmanu D.I.C Lover Reputation: 966 Posts: 2,362 Joined: 04-December 09 Re: g++ undefined reference with class template Posted 12 August 2010 - 12:57 PM Because templated functions (applies to member functions too) must be implemented in the same unit where they are prototyped. So, move the entire code from list.cpp to list.h and it will undefined reference to work. Though, you can use some ways to implement templated functions in another source file (though, I'll never use them). Read here, from 35.12. This post has been edited by sarmanu: 12 August 2010 - 12:58 PM Was This Post Helpful? 3 Back to top MultiQuote Quote + Reply #3 JackOfAllTrades Saucy! Reputation: 6243 Posts: 24,012 Joined: 23-August 08 Re: g++ undefined reference with class template Posted 12 August 2010 - 12:58 PM I'm no template expert, but I believe you need to implement the templated method in the header file, not in a separate code file. If I do this, it works for me. EDIT: Ninja'd!! This post has been edited by JackOfAllTrades: 12 August 2010 - 12:58 PM Was This Post Helpful? 1 Back to top MultiQuote Quote + Reply #4 jjl Engineer Reputation: 1179 Posts: 4,817 Joined: 09-June 09 Re: g++ undefined reference with class template Posted 12 August 2010 - 02:03 PM Or if you know the specific cases of the templete types you will be dealing with you can use template instantiation. That is if you are seperating your templated functions from your header file where they were declared example list.h template class List { public: char Get( void ) const; }; list.cpp #include "list.hh" template class List; //this is what you need template char List::Get(