+ 2

Undefined Reference Help

Im trying to practice the Class Templates example but i get an error in code blocks with the gnu gcc compiler: Here are my files: main.cpp: #include <iostream> #include "Pair.h" int main() { Pair <int> obj(11, 22); //undefined reference to 'Pair<int>::Pair(int,int) std::cout << obj.bigger() << std::endl; //undefined reference to 'Pair<int>::bigger()' return 0; } Pair.h (header and implementation files are in the same folder) : #ifndef PAIR_H #define PAIR_H template <class T> class Pair { private: T first, second; public: Pair (T a,T b); T bigger(); }; #endif // PAIR_H Pair.cpp: #include "Pair.h" template <class T> Pair<T>::Pair(T a,T b): first(a), second(b) { } template <class T> T Pair<T>::bigger() { return (first>second ? first : second); } So why am i getting those errors

23rd Apr 2017, 5:33 PM
Alexandre Lessard
8 ответов
+ 7
I have compiled all your files into one code. #include <iostream> template <class T> class Pair { private: T first, second; public: Pair (T a,T b); T bigger(); }; template <class T> Pair<T>::Pair(T a,T b): first(a), second(b) { } template <class T> T Pair<T>::bigger() { return (first>second ? first : second); } int main() { Pair <int> obj(11, 22); //undefined reference to 'Pair<int>::Pair(int,int) std::cout << obj.bigger() << std::endl; //undefined reference to 'Pair<int>::bigger()' return 0; } The thing is, the above code works on SL! One thing I can say is that there are problems when people deal with templates and try to separate the class definition and declarations into header and cpp files. I'm not sure why. Try placing your cpp content into your header.
23rd Apr 2017, 5:56 PM
Hatsy Rei
Hatsy Rei - avatar
23rd Apr 2017, 6:02 PM
Hatsy Rei
Hatsy Rei - avatar
+ 2
You ha e to tell the compiler which files to link. Check this out: http://www.network-theory.co.uk/docs/gccintro/gccintro_11.html
23rd Apr 2017, 6:01 PM
Bebida Roja
Bebida Roja - avatar
+ 2
@Alexandre Lessard You can still seperate the declaration and definition by including the file after the class declaration: //main.cpp #include "MyClass.h" int main() { MyClass<int> m; } //MyClass.h #ifndef MYCLASS_H #define MYCLASS_H template< typename T > class MyClass { public: MyClass(); private: T a; }; #include "MyClass.inl" #endif // MYCLASS_H //MyClass.inl template<typename T> MyClass<T>::MyClass() { }
23rd Apr 2017, 6:45 PM
Dennis
Dennis - avatar
+ 1
Im not sure but try to put T first; T second; instead of T first, second; Also, make sure all your files are linked.
23rd Apr 2017, 5:39 PM
Bebida Roja
Bebida Roja - avatar
+ 1
Thanks for the information Hatsu i added this line of code: template class Pair<int>; at the end of my Pair.cpp file and it compiled with no error, as explained in the link you posted
23rd Apr 2017, 6:45 PM
Alexandre Lessard
0
what do you mean by linked i included them and they are in the same file
23rd Apr 2017, 5:54 PM
Alexandre Lessard
0
i just tried it and it worked, thank you, but im still wondering is it something thats doable to seperate the class definition and declaration when making a class template
23rd Apr 2017, 6:06 PM
Alexandre Lessard