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-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
>
|