Thread: [Dev-C++] templates + linker error
Open Source C & C++ IDE for Windows
Brought to you by:
claplace
From: Jorge G. <jor...@gm...> - 2008-05-25 21:42:27
|
i have 3 source calc.h calc.cpp and principal.cpp (i compilated it with devcpp) i have this error: In function `main': [Linker error] undefined reference to `calc<double>::multiply(double, double)' ld returned 1 exit status [Build Error] ["Proyecto] Error 1 somebody could help me please :) /********calc.h******/ #ifndef _CALC_H #define _CALC_H template <class A_Type> class calc { public: A_Type multiply(A_Type x, A_Type y); A_Type add(A_Type x, A_Type y); }; #endif /****calc.cpp******/ #include "calc.h" #include <iostream> using namespace std; template <class A_Type> A_Type calc<A_Type>::multiply(A_Type x,A_Type y) { return x*y; } template <class A_Type> A_Type calc<A_Type>::add(A_Type x, A_Type y) { return x+y; } /******principal.cpp*****/ #include "calc.h" #include <iostream> #include <cstdlib> using namespace std; int main() { calc <double> a_calc_class; double a, b; a=4.3; b=3.4; a_calc_class.multiply(a,b); system("PAUSE"); return EXIT_SUCCESS; } /***************************/ |
From: Lloyd <ll...@cd...> - 2008-05-26 09:52:20
|
The reason for the error is you are not passing the obj file of calc.cpp to the linker. Make appropriate settings in DevCpp. I think, you have to add calc.cpp to your project. Sorry I dont use DevCpp. On Sun, 2008-05-25 at 16:42 -0500, Jorge Guevara wrote: > > i have 3 source calc.h calc.cpp and principal.cpp (i compilated it > with devcpp) > > i have this error: > In function `main': > [Linker error] undefined reference to `calc<double>::multiply(double, > double)' > ld returned 1 exit status > [Build Error] ["Proyecto] Error 1 > > somebody could help me please :) > > /********calc.h******/ > #ifndef _CALC_H > #define _CALC_H > > template <class A_Type> class calc > { > public: > A_Type multiply(A_Type x, A_Type y); > A_Type add(A_Type x, A_Type y); > > }; > > #endif > > /****calc.cpp******/ > #include "calc.h" > #include <iostream> > using namespace std; > > template <class A_Type> A_Type calc<A_Type>::multiply(A_Type x,A_Type > y) > { > return x*y; > } > template <class A_Type> A_Type calc<A_Type>::add(A_Type x, A_Type y) > { > return x+y; > } > > > /******principal.cpp*****/ > #include "calc.h" > #include <iostream> > #include <cstdlib> > > using namespace std; > > int main() > { > calc <double> a_calc_class; > double a, b; > a=4.3; > b=3.4; > a_calc_class.multiply(a,b); > > system("PAUSE"); > return EXIT_SUCCESS; > } > /***************************/ > > > ------------------------------------------------------------------------- > This SF.net email is sponsored by: Microsoft > Defy all challenges. Microsoft(R) Visual Studio 2008. > http://clk.atdmt.com/MRT/go/vse0120000070mrt/direct/01/ > _______________________________________________ Dev-cpp-users mailing list Dev...@li... TO UNSUBSCRIBE: http://www23.brinkster.com/noicys/devcpp/ub.htm https://lists.sourceforge.net/lists/listinfo/dev-cpp-users ______________________________________ Scanned and protected by Email scanner |
From: Per W. <pw...@ia...> - 2008-05-26 18:57:18
|
Think a bit about it. What code to you suggest that the compiler should enerate when it compiles calc.cpp? If the compiler does not know that other source files will use the template parameter "double", how will the compiler then be able to add a "double" version of multiply when it compiles calc.cpp? Remember that it is the compiler, not the linker, that produces the runnable code. The linker will have no use for an object file that doesn't contain instantiated template functions. /pwm On Mon, 26 May 2008, Lloyd wrote: > The reason for the error is you are not passing the obj file of calc.cpp > to the linker. Make appropriate settings in DevCpp. I think, you have to > add calc.cpp to your project. Sorry I dont use DevCpp. > > > On Sun, 2008-05-25 at 16:42 -0500, Jorge Guevara wrote: > > > > i have 3 source calc.h calc.cpp and principal.cpp (i compilated it > > with devcpp) > > > > i have this error: > > In function `main': > > [Linker error] undefined reference to `calc<double>::multiply(double, > > double)' > > ld returned 1 exit status > > [Build Error] ["Proyecto] Error 1 > > > > somebody could help me please :) > > > > /********calc.h******/ > > #ifndef _CALC_H > > #define _CALC_H > > > > template <class A_Type> class calc > > { > > public: > > A_Type multiply(A_Type x, A_Type y); > > A_Type add(A_Type x, A_Type y); > > > > }; > > > > #endif > > > > /****calc.cpp******/ > > #include "calc.h" > > #include <iostream> > > using namespace std; > > > > template <class A_Type> A_Type calc<A_Type>::multiply(A_Type x,A_Type > > y) > > { > > return x*y; > > } > > template <class A_Type> A_Type calc<A_Type>::add(A_Type x, A_Type y) > > { > > return x+y; > > } > > > > > > /******principal.cpp*****/ > > #include "calc.h" > > #include <iostream> > > #include <cstdlib> > > > > using namespace std; > > > > int main() > > { > > calc <double> a_calc_class; > > double a, b; > > a=4.3; > > b=3.4; > > a_calc_class.multiply(a,b); > > > > system("PAUSE"); > > return EXIT_SUCCESS; > > } > > /***************************/ > > > > > > ------------------------------------------------------------------------- > > This SF.net email is sponsored by: Microsoft > > Defy all challenges. Microsoft(R) Visual Studio 2008. > > http://clk.atdmt.com/MRT/go/vse0120000070mrt/direct/01/ > > _______________________________________________ Dev-cpp-users mailing list Dev...@li... TO UNSUBSCRIBE: http://www23.brinkster.com/noicys/devcpp/ub.htm https://lists.sourceforge.net/lists/listinfo/dev-cpp-users > > > ______________________________________ > Scanned and protected by Email scanner > > ------------------------------------------------------------------------- > This SF.net email is sponsored by: Microsoft > Defy all challenges. Microsoft(R) Visual Studio 2008. > http://clk.atdmt.com/MRT/go/vse0120000070mrt/direct/01/ > _______________________________________________ > Dev-cpp-users mailing list > Dev...@li... > TO UNSUBSCRIBE: http://www23.brinkster.com/noicys/devcpp/ub.htm > https://lists.sourceforge.net/lists/listinfo/dev-cpp-users > |
From: Per W. <pw...@ia...> - 2008-05-26 16:40:39
|
The compiler can't create the code for a template function unless it sees the implementation when you try to use the template function. You should have the implementation of your multiply function in the header file, and not hidden in a cpp file. It is only the linker that will see the generated code from multiple cpp files, and neither the compiler nor the compiler knows that calc.cpp should create a specific version of the multiply function with the double data type. /pwm On Sun, 25 May 2008, Jorge Guevara wrote: > i have 3 source calc.h calc.cpp and principal.cpp (i compilated it with > devcpp) > > i have this error: > In function `main': > [Linker error] undefined reference to `calc<double>::multiply(double, > double)' > ld returned 1 exit status > [Build Error] ["Proyecto] Error 1 > > somebody could help me please :) > > /********calc.h******/ > #ifndef _CALC_H > #define _CALC_H > > template <class A_Type> class calc > { > public: > A_Type multiply(A_Type x, A_Type y); > A_Type add(A_Type x, A_Type y); > > }; > > #endif > > /****calc.cpp******/ > #include "calc.h" > #include <iostream> > using namespace std; > > template <class A_Type> A_Type calc<A_Type>::multiply(A_Type x,A_Type y) > { > return x*y; > } > template <class A_Type> A_Type calc<A_Type>::add(A_Type x, A_Type y) > { > return x+y; > } > > > /******principal.cpp*****/ > #include "calc.h" > #include <iostream> > #include <cstdlib> > > using namespace std; > > int main() > { > calc <double> a_calc_class; > double a, b; > a=4.3; > b=3.4; > a_calc_class.multiply(a,b); > > system("PAUSE"); > return EXIT_SUCCESS; > } > /***************************/ > |
From: Jorge G. <jor...@gm...> - 2008-05-27 20:17:32
|
I try with this. and no problem #include <iostream> #include <cstdlib> #include "mypair.hpp" #include "mypair.cpp" // i include the implementation in the source principal.cpp the question is less efficient?, or its ok? bytes 2008/5/26, Per Westermark <pw...@ia...>: > > The compiler can't create the code for a template function unless it sees > the implementation when you try to use the template function. > > You should have the implementation of your multiply function in the header > file, and not hidden in a cpp file. > > It is only the linker that will see the generated code from multiple cpp > files, and neither the compiler nor the compiler knows that calc.cpp > should create a specific version of the multiply function with the double > data type. > > /pwm > > > On Sun, 25 May 2008, Jorge Guevara wrote: > > > i have 3 source calc.h calc.cpp and principal.cpp (i compilated it with > > devcpp) > > > > i have this error: > > In function `main': > > [Linker error] undefined reference to `calc<double>::multiply(double, > > double)' > > ld returned 1 exit status > > [Build Error] ["Proyecto] Error 1 > > > > somebody could help me please :) > > > > /********calc.h******/ > > #ifndef _CALC_H > > #define _CALC_H > > > > template <class A_Type> class calc > > { > > public: > > A_Type multiply(A_Type x, A_Type y); > > A_Type add(A_Type x, A_Type y); > > > > }; > > > > #endif > > > > /****calc.cpp******/ > > #include "calc.h" > > #include <iostream> > > using namespace std; > > > > template <class A_Type> A_Type calc<A_Type>::multiply(A_Type x,A_Type y) > > { > > return x*y; > > } > > template <class A_Type> A_Type calc<A_Type>::add(A_Type x, A_Type y) > > { > > return x+y; > > } > > > > > > /******principal.cpp*****/ > > #include "calc.h" > > #include <iostream> > > #include <cstdlib> > > > > using namespace std; > > > > int main() > > { > > calc <double> a_calc_class; > > double a, b; > > a=4.3; > > b=3.4; > > a_calc_class.multiply(a,b); > > > > system("PAUSE"); > > return EXIT_SUCCESS; > > } > > /***************************/ > > > > -- Ing. Jorge Luis Guevara Diaz Bs Ciencias de la Computacion Maestrante en Ciencia de la Computación http://jorge.sistemasyservidores.com http://inf.unitru.edu.pe/~jlgd Escuela de Informática - Universidad Nacional de Trujillo Escuela de Ingeniería de Sistemas - Universidad Privada del Norte |
From: Per W. <pw...@ia...> - 2008-05-28 04:36:42
|
Always avoid including cpp files. cpp files are indented to generate object files and be linked. Templated functions are intended to be compiled into object files first when needed. As I wrote in one of my earlier mails, template functions should be placed in the relevant header file. Look at the STL files: In all cases, the actual source code for the implementations of lists, trees, arrays etc are available in the header files. /pwm On Tue, 27 May 2008, Jorge Guevara wrote: > I try with this. and no problem > > #include <iostream> > #include <cstdlib> > #include "mypair.hpp" > #include "mypair.cpp" // i include the implementation in the source > principal.cpp > > the question is less efficient?, or its ok? > > bytes > > > 2008/5/26, Per Westermark <pw...@ia...>: > > > > The compiler can't create the code for a template function unless it sees > > the implementation when you try to use the template function. > > > > You should have the implementation of your multiply function in the header > > file, and not hidden in a cpp file. > > > > It is only the linker that will see the generated code from multiple cpp > > files, and neither the compiler nor the compiler knows that calc.cpp > > should create a specific version of the multiply function with the double > > data type. > > > > /pwm > > > > > > On Sun, 25 May 2008, Jorge Guevara wrote: > > > > > i have 3 source calc.h calc.cpp and principal.cpp (i compilated it with > > > devcpp) > > > > > > i have this error: > > > In function `main': > > > [Linker error] undefined reference to `calc<double>::multiply(double, > > > double)' > > > ld returned 1 exit status > > > [Build Error] ["Proyecto] Error 1 > > > > > > somebody could help me please :) > > > > > > /********calc.h******/ > > > #ifndef _CALC_H > > > #define _CALC_H > > > > > > template <class A_Type> class calc > > > { > > > public: > > > A_Type multiply(A_Type x, A_Type y); > > > A_Type add(A_Type x, A_Type y); > > > > > > }; > > > > > > #endif > > > > > > /****calc.cpp******/ > > > #include "calc.h" > > > #include <iostream> > > > using namespace std; > > > > > > template <class A_Type> A_Type calc<A_Type>::multiply(A_Type x,A_Type y) > > > { > > > return x*y; > > > } > > > template <class A_Type> A_Type calc<A_Type>::add(A_Type x, A_Type y) > > > { > > > return x+y; > > > } > > > > > > > > > /******principal.cpp*****/ > > > #include "calc.h" > > > #include <iostream> > > > #include <cstdlib> > > > > > > using namespace std; > > > > > > int main() > > > { > > > calc <double> a_calc_class; > > > double a, b; > > > a=4.3; > > > b=3.4; > > > a_calc_class.multiply(a,b); > > > > > > system("PAUSE"); > > > return EXIT_SUCCESS; > > > } > > > /***************************/ > > > > > > > > > > -- > Ing. Jorge Luis Guevara Diaz > Bs Ciencias de la Computacion > Maestrante en Ciencia de la Computación > http://jorge.sistemasyservidores.com http://inf.unitru.edu.pe/~jlgd > Escuela de Informática - Universidad Nacional de Trujillo > Escuela de Ingeniería de Sistemas - Universidad Privada del Norte > |