By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
445,852 Members | 2,219 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 445,852 IT Pros & Developers. It's quick & easy.

g++ troubles with templates

P: n/a
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<doubleI;

unsigned int L;

unsigned int N,R;

double a;
double Tx;

unsigned int M;
t_Data *P_M;

PointField<t_Datatarget;
PointField<t_Datasource;
PointField<t_Datapsf;

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

==============================

Feb 20 '07 #1
Share this Question
Share on Google+
4 Replies


P: n/a
WillyFoobar wrote:
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<doubleI;

unsigned int L;

unsigned int N,R;

double a;
double Tx;

unsigned int M;
t_Data *P_M;

PointField<t_Datatarget;
PointField<t_Datasource;
PointField<t_Datapsf;

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. :)

Feb 20 '07 #2

P: n/a
WillyFoobar wrote:
[template linker error redacted]
Piyo gave you the answer, but to give a bit more reference,

It's a FAQ. http://www.parashift.com/c++-faq-lit...html#faq-35.12

Actually, it's a series of FAQS. 35.12 through about 35.15.
Feb 20 '07 #3

P: n/a
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
====================

Feb 21 '07 #4

P: n/a
WillyFoobar wrote:
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
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_
Feb 21 '07 #5

This discussion thread is closed

Replies have been disabled for this discussion.