Error Undefined Reference To Template Function
here for a quick overview of the site Help Center Detailed answers to any questions you might have Meta Discuss the workings and policies of this site
Undefined Reference To Template Function C++
About Us Learn more about Stack Overflow the company Business Learn more about undefined reference to template member function hiring developers or posting ads with us Stack Overflow Questions Jobs Documentation Tags Users Badges Ask Question x Dismiss
Undefined Reference To Template Class Function
Join the Stack Overflow Community Stack Overflow is a community of 4.7 million programmers, just like you, helping each other. Join them; it only takes a minute: Sign up Undefined reference error for c++ undefined reference to template class method template method up vote 31 down vote favorite 7 This has been driving me mad for the past hour and a half. I know it's a small thing but cannot find what's wrong (the fact that it's a rainy Friday afternoon, of course, does not help). I have defined the following class that will hold configuration parameters read from a file and will let me undefined reference to template specialization access them from my program: class VAConfig { friend std::ostream& operator<<( std::ostream& lhs, const VAConfig& rhs); private: VAConfig(); static std::string configFilename; static VAConfig* pConfigInstance; static TiXmlDocument* pXmlDoc; std::map valueHash; public: static VAConfig* getInstance(); static void setConfigFileName( std::string& filename ) { configFilename = filename; } virtual ~VAConfig(); void readParameterSet( std::string parameterGroupName ); template T readParameter( const std::string parameterName ); template T convert( const std::string& value ); }; where the method convert() is defined in VAConfig.cpp as template T VAConfig::convert( const std::string& value ) { T t; std::istringstream iss( value, std::istringstream::in ); iss >> t; return t; } All quite simple. But when I test from my main program using int y = parameters->convert("5"); I get an undefined reference to 'int VAConfig::convert...' compilation error. Ditto for readParameter(). Looked at a lot of template tutorials but coul not figure this out. Any ideas? c++ templates compilation linker share|improve this question edited Aug 16 '12 at 17:15 curiousguy 3,6722126 asked Jul 10 '09 at 19:13 recipriversexclusion 1,83031840 2 An hour and a half isn't so bad...it killed me for 3 yesterday. –Keith Pinson Aug 16 '12 at 16:24 T
here for a quick overview of the site Help Center Detailed answers to any questions you might have Meta Discuss the workings and policies of this site About Us Learn more about Stack Overflow the
Template Class Implementation In Cpp File
company Business Learn more about hiring developers or posting ads with us Stack Overflow extern template Questions Jobs Documentation Tags Users Badges Ask Question x Dismiss Join the Stack Overflow Community Stack Overflow is a community of 4.7
Template Instantiation
million programmers, just like you, helping each other. Join them; it only takes a minute: Sign up C++ class template undefined reference to function up vote 4 down vote favorite I keep getting undefined reference when http://stackoverflow.com/questions/1111440/undefined-reference-error-for-template-method i call the two functions from my template class "add" and "greater" in my main function. So, i have: number.h #ifndef NUMBER_H #define NUMBER_H template class number { public: T x; T y; number (int a, int b){ x=a; y=b;} int add (T&); T greater (); }; #endif number.cpp #include "number.h" template int number::add (T& rezAdd){ rezAdd = x+y; return 1; } template T number::greater (){ return http://stackoverflow.com/questions/22595015/c-class-template-undefined-reference-to-function x>y? x : y; } And my main file is: resolver.cpp #include #include #include "number.h" int main (int argc, char **argv) { int aux; number c(3,5); c.add(aux); printf ("number added [%d]\n", c.add(aux)); printf ("greater number: [%d]\n", c.greater()); return 0; } The errors that i keep getting are: g++ -Wall -o tema1 resolver.cpp number.cpp /tmp/ccX483J4.o: In function `main': resolver.cpp:(.text+0x34): undefined reference to `number::add(int&)' resolver.cpp:(.text+0x47): undefined reference to `number::add(int&)' resolver.cpp:(.text+0x64): undefined reference to `number::greater()' collect2: ld returned 1 exit status make: *** [all] Error 1 Thanks for the help in advance! c++ c class undefined-reference template-classes share|improve this question edited Mar 23 '14 at 18:26 asked Mar 23 '14 at 18:18 MihaiGrad 3015 add a comment| 3 Answers 3 active oldest votes up vote 4 down vote accepted Your class is named wrong. Your class is named cai where all your functions belong to a class named number: http://ideone.com/ZayX0c One more thing.. you cannot have templates in the .cpp file. Template functions/defintions go in the header along with the class declaration. This is the reason for your undefined function error. Non-template functions go in the .cpp. #include #include template class number { public: T x; T y; number (int a, int b){ x=a; y=b;} int add (T&); T greater (); }; tem
here for a quick overview of the site Help Center Detailed answers to any questions you might have Meta Discuss the workings and policies of this site About Us Learn more http://stackoverflow.com/questions/8752837/undefined-reference-to-template-class-constructor about Stack Overflow the company Business 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 Overflow Community Stack Overflow is a community of 4.7 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 undefined reference vote favorite 66 This question already has an answer here: Why can templates 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 undefined reference to 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(T&); T saca(); void print() const; virtual ~cola(); }; #endif // COLA_H "cola.cpp": #include "cola.h" #include "nodo_colaypila.h" #include using namespace std; template cola::cola() { pri = NULL; ult = NULL;//ctor } template void cola::anade(T& valor) { nodo_colaypila * nuevo; if (ult) { nuevo = new nodo_colaypila (valor); ult->sig = nuevo; ult = nuevo; } if (!pri) { pri = nuevo; } } template T cola::saca() { nodo_colaypila * aux; T valor; aux = pri; if (!aux) { return 0; } pri = aux->sig; valor = aux->elem; delete aux; if(!pri) { ult = NULL; } return valor; } template cola::~cola() { while(pri) {