0

C++ Undefined Reference to... using Templates

Hello Everyone! I'm studying C++ and when I try to compile all the knowledge learned from the c++ course that I finished here in sololearn, in one single and simple problem I stuck "HEHEHE". thats my Three files... Number 1: Calculadora.h (Calculator) ******************************************************************** #ifndef CALCULADORA_H #define CALCULADORA_H template<typename T> class Calculadora { public: //Constructor Calculadora(){} //Funções T somar( T, T ); T subtrair( T, T ); T multiplicar( T, T ); T dividir( T, T ); void mostrarResultado( T ); }; #endif ******************************************************************** Number 2: Calculadora.cpp ******************************************************************** #include<iostream> #include"Calculadora.h" template <typename T> T Calculadora<T>::somar(T n1, T n2) { return (n1 + n2); } template <typename T> T Calculadora<T>::subtrair(T n1, T n2) { return (n1 - n2); } template <typename T> T Calculadora<T>::multiplicar(T n1, T n2) { return (n1 * n2); } template <typename T> T Calculadora<T>::dividir(T n1, T n2) { return (n1 / n2); } template <typename T> void Calculadora<T>::mostrarResultado(T res) { std::cout << res << std::endl; } ******************************************************************** Number 3: main.cpp ******************************************************************** #include <iostream> #include "Calculadora.h" int num1 = 0, num2 = 0; int main() { Calculadora <int> cObj; std::cout << "Primeiro Valor: "; std::cin >> num1; std::cout << std::endl; std::cout << "Segundo Valor: "; std::cin >> num2; std::cout << std::endl; std::cout << "Resultado da Soma = " << cObj.somar(num1, num2) << std::endl; std::cout << "Resultado da Subtração = " << cObj.subtrair(num1, num2) << std::endl; std::cout << "Resultado da Multiplicação = " << cObj.multiplicar(num1, num2) << std::enl;

27th Feb 2020, 11:21 PM
Murilo Bueno Julião Lemos
Murilo Bueno Julião Lemos - avatar
6 odpowiedzi
+ 1
Thanks, Ipang, but the link is broken! I'll see how to do that and repost the code. Mohamed ELomari, thanks also, man! In this case I'm using the template to not use anyone kind of explicity intantiate. I try to put all the code in one file and just works, the error code is probably a linker issue. Putting in separate files occurs this debbug message: - undefined reference to 'Calculadora<int>::somar(int,int)'
28th Feb 2020, 12:37 AM
Murilo Bueno Julião Lemos
Murilo Bueno Julião Lemos - avatar
+ 1
https://code.sololearn.com/cesaPOXebShE This is the link for my code! :D
28th Feb 2020, 12:38 AM
Murilo Bueno Julião Lemos
Murilo Bueno Julião Lemos - avatar
+ 1
Murilo Bueno Julião Lemos I tested the code with int and double and it works just fine. I'm not sure why you encountered an issue before, hope others can help you further. Anyways, Good job! 👍 The link isn't broken buddy, but it can't be opened in browser, it can only be opened in SoloLearn mobile app. I assume you are using SoloLearn website, that makes sense. But no problem, I see you figured out how to link your code 👌
28th Feb 2020, 12:52 AM
Ipang
+ 1
Not sure if this is solved yet, but template class definitions go in the header file, not the cpp file because a template class is not an actual class. It tells the compiler how to generate one and for it to do that the compiler has to see the entire thing. If it is in the cpp file then you will have to explicitly tell the compiler which types it should generate like Mohamed ELomari said.
28th Feb 2020, 8:27 AM
Dennis
Dennis - avatar
0
Prefer to save the code in SoloLearn and share the code link when the code is big (more than 10-15 lines). Attaching code as raw text risks the code to get truncated from surpassing character limits, as you see above. * Follow this guide to sharing links in case you didn't know yet. https://www.sololearn.com/post/74857/?ref=app
27th Feb 2020, 11:31 PM
Ipang
0
you have to explicitly instantiate the template by adding the following line at the end of Calculadora.cpp: template class Calculadora<int>; note: you have to do that for every type T you intend to use with Calculadora<T>
28th Feb 2020, 12:16 AM
MO ELomari
MO ELomari - avatar