g++ troubles with templates

W

WillyFoobar

Hello,
I have trouble with g++ code related to templates in program of
mine,

perhaps anybody can help me with that.
I might am doing something wrong with the template names....


Compiling my code, I get the compiler error:
(Yes, I broke the compiler outputs in order to make it more readable)

What is wrong?
Willy

-----------------------------
g++ -g -I. -D___x86___ -D_DEBUG -Wall MainAsx.o PCMFile.o asxio.o
\ FhxError.o Unix4Win.o perrorf.o \
ftt_recursive.o ftt_heterogen.o ftt_base.o \
pointfield.o pointfield_deconvolute.o pointfield_convolute.o -
o asx -lm

MainAsx.o: In function `CalcHFtt2Asx(unsigned int, unsigned int,
double, char*, char*, bool)':
/home/duffy/hafi/Documents/prj/SCIENTIA/PHYSIK/ASX/asx/sw/MainAsx.cpp:
48:
undefined reference to
`FttRecursive<std::complex<double>>::FttRecursive(unsigned int,
unsigned int, double, double)'
collect2: ld returned 1 exit status
make: *** [asx] Fehler 1

------------------------------
------------------------------ I initiate the FttRecursive herein:

#include <stdio.h>
#include <string.h>
#include <stdlib.h>

#include <math.h>
#include <complex>

#include "Unix4Win.h"
#include "ftt.h" //this includes both ftt_base.h and ftt_recursive.h


long FrameCount=0;


int CalcHFtt2Asx(unsigned int N,
unsigned int R,
double a ,
char *input,
char *output,
bool recursive)
{
/* .... */
t_ptr_FttBase ftt;
if(recursive) ftt=new FTT(N,R,a,Tx);
else /*...*/
/*...*/
}
===========================

----------------------------- in file ftt_recursive.h I declare:

#ifndef __FTT_RECURSIVE_H
#define __FTT_RECURSIVE_H

#include <complex>
#include <math.h>

#include "ftt_base.h"
template<class t_Data >
class FttRecursive
: public FttBase<t_Data >
{
public:
FttRecursive(unsigned int N=32, unsigned int R=32,
double a=0.010, double Tx=1.0/11025 );
virtual ~FttRecursive() {};

virtual t_Data *const calc(double *pcm);

protected:
const double E;
};

typedef FttRecursive<std::complex<double> > FTT;

#endif
===========================
------------------------------ in file ftt_recursive.cpp i define:
#include <math.h>
#include <complex>


#include "ftt_recursive.h"

template<class t_Data >
FttRecursive<t_Data >::FttRecursive(unsigned int N_, unsigned int R_,
double a_, double Tx_)
: FttBase<t_Data>( N_, R_, a_, Tx_)
, E(exp(-a_*Tx_))
{

}

===========================
----------------------------- in file ftt_base.h I declare:
#ifndef __FTT_BASE_H
#define __FTT_BASE_H

#include <complex>
#include <math.h>

#include "pointfield.h"

template<class t_Data>
class FttBase
{
public:
FttBase(unsigned int N=32, unsigned int R=32, double
a=0.010, double Tx=1.0/11025);
virtual ~FttBase();

virtual t_Data *const calc(double *pcm)=0;

t_Data* const convolute(t_Data* con_in_spectrum);



protected:
const std::complex<double> I;

unsigned int L;

unsigned int N,R;

double a;
double Tx;

unsigned int M;
t_Data *P_M;

PointField<t_Data> target;
PointField<t_Data> source;
PointField<t_Data> psf;

bool create_psf(PointField<t_Data> &PSF);

t_Data psf_value_function(double delta_w, double delta_t);
};


typedef FttBase<std::complex<double> > *t_ptr_FttBase;

#endif

==============================
 
P

Piyo

WillyFoobar said:
Hello,
I have trouble with g++ code related to templates in program of
mine,

perhaps anybody can help me with that.
I might am doing something wrong with the template names....


Compiling my code, I get the compiler error:
(Yes, I broke the compiler outputs in order to make it more readable)

What is wrong?
Willy

-----------------------------
g++ -g -I. -D___x86___ -D_DEBUG -Wall MainAsx.o PCMFile.o asxio.o
\ FhxError.o Unix4Win.o perrorf.o \
ftt_recursive.o ftt_heterogen.o ftt_base.o \
pointfield.o pointfield_deconvolute.o pointfield_convolute.o -
o asx -lm

MainAsx.o: In function `CalcHFtt2Asx(unsigned int, unsigned int,
double, char*, char*, bool)':
/home/duffy/hafi/Documents/prj/SCIENTIA/PHYSIK/ASX/asx/sw/MainAsx.cpp:
48:
undefined reference to
`FttRecursive<std::complex<double>>::FttRecursive(unsigned int,
unsigned int, double, double)'
collect2: ld returned 1 exit status
make: *** [asx] Fehler 1

------------------------------
------------------------------ I initiate the FttRecursive herein:

#include <stdio.h>
#include <string.h>
#include <stdlib.h>

#include <math.h>
#include <complex>

#include "Unix4Win.h"
#include "ftt.h" //this includes both ftt_base.h and ftt_recursive.h


long FrameCount=0;


int CalcHFtt2Asx(unsigned int N,
unsigned int R,
double a ,
char *input,
char *output,
bool recursive)
{
/* .... */
t_ptr_FttBase ftt;
if(recursive) ftt=new FTT(N,R,a,Tx);
else /*...*/
/*...*/
}
===========================

----------------------------- in file ftt_recursive.h I declare:

#ifndef __FTT_RECURSIVE_H
#define __FTT_RECURSIVE_H

#include <complex>
#include <math.h>

#include "ftt_base.h"
template<class t_Data >
class FttRecursive
: public FttBase<t_Data >
{
public:
FttRecursive(unsigned int N=32, unsigned int R=32,
double a=0.010, double Tx=1.0/11025 );
virtual ~FttRecursive() {};

virtual t_Data *const calc(double *pcm);

protected:
const double E;
};

typedef FttRecursive<std::complex<double> > FTT;

#endif
===========================
------------------------------ in file ftt_recursive.cpp i define:
#include <math.h>
#include <complex>


#include "ftt_recursive.h"

template<class t_Data >
FttRecursive<t_Data >::FttRecursive(unsigned int N_, unsigned int R_,
double a_, double Tx_)
: FttBase<t_Data>( N_, R_, a_, Tx_)
, E(exp(-a_*Tx_))
{

}

===========================
----------------------------- in file ftt_base.h I declare:
#ifndef __FTT_BASE_H
#define __FTT_BASE_H

#include <complex>
#include <math.h>

#include "pointfield.h"

template<class t_Data>
class FttBase
{
public:
FttBase(unsigned int N=32, unsigned int R=32, double
a=0.010, double Tx=1.0/11025);
virtual ~FttBase();

virtual t_Data *const calc(double *pcm)=0;

t_Data* const convolute(t_Data* con_in_spectrum);



protected:
const std::complex<double> I;

unsigned int L;

unsigned int N,R;

double a;
double Tx;

unsigned int M;
t_Data *P_M;

PointField<t_Data> target;
PointField<t_Data> source;
PointField<t_Data> psf;

bool create_psf(PointField<t_Data> &PSF);

t_Data psf_value_function(double delta_w, double delta_t);
};


typedef FttBase<std::complex<double> > *t_ptr_FttBase;

#endif

==============================
You will need to copy the entire contents of
ftt_recursive.cpp to ftt_recursive.h since
the template export keyword has not been implemented
by the GNU folks.

Or you can #include "ftt_recursive.cpp" into
ftt_recursive.h. I normally do not like this but the
GNU folks do it quite often in the standard headers. :)
 
W

WillyFoobar

Thank you to "Piyo" and "red floyd"

The tip and the faq were helpfull.

Here is what I needed to do to get it working.

I added #cpp commndo lines like at all *.cpp source and *.h header
files
---------------------------------------
#ifndef __FTT_BASE_CPP
#define __FTT_BASE_CPP

/*..*/
#endif
====================
I also added in each header file a inclusion to the related cpp-files
and a dummy template declaration

---------------------------------------
#ifndef __FTT_BASE_H
#define __FTT_BASE_H


/*...*/

#include "ftt_base.cpp"
template class FttBase<std::complex<double> >;
#endif
====================
 
R

red floyd

WillyFoobar said:
Thank you to "Piyo" and "red floyd"

The tip and the faq were helpfull.

Here is what I needed to do to get it working.

I added #cpp commndo lines like at all *.cpp source and *.h header
files

Those are VERY bad include guards. Any identifier with two consecutive
underscores is reserved to the implementation. That means you may not
define it for your own use. Lest you think about just removing one, any
identifier with a leading underscore followed by an upper case letter is
similarly reserved to the implementation.

I would use

#ifndef FTT_BASE_CPP_
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,776
Messages
2,569,603
Members
45,216
Latest member
topweb3twitterchannels

Latest Threads

Top