Re: [Dev-C++] templates + linker error
Open Source C & C++ IDE for Windows
Brought to you by:
claplace
|
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;
> }
> /***************************/
>
|