Could you help me to find out what is wrong the following usage of function object

P

PengYu.UT

Hi All,

I want to use scrJ_minus_O as the function to fill in the array "a" in
population_decomposition. But the g++ compiler always says the line "
pop_dec(scrJ_minus_O<double>(.5));" is wrong.

main.cc: In function `int main(int, char **)':
main.cc:39: no matching function for call to `pop_dec
(scrJ_minus_O<double>)'

Would you please tell me how I should change line 39? Thanks!

Best wishes,
Peng


#include <iostream>
#include <functional>
#include <cmath>

template <class _Tp>
class scrJ_minus_O : public binary_function<_Tp, _Tp, _Tp>{
public:
scrJ_minus_O(_Tp sigma) : _sigma(sigma) {};
_Tp operator()(const _Tp f, const _Tp g){
if((f * f + g * g) < _sigma * _sigma)
return 1. / (M_PI * _sigma * _sigma);
else
return 0.;
}
private:
_Tp _sigma;
};

template <class _Tp, class _Generator>
class population_decomposition {
public:
population_decomposition(const _Generator& generator) :
_generator(generator) {
for(int i = 0; i < 10; i ++){
a = _generator(.1 * i, .1 * i);
}
};
_Tp a[10];
private:
_Generator _generator;
};

template <class _Tp, class _Generator>
inline population_decomposition<_Tp, _Generator>
pop_dec(const _Generator& generator){
return population_decomposition<_Tp,_Generator>(generator);
}

main(int argc, char *argv[]){
pop_dec(scrJ_minus_O<double>(.5));
}
 
J

John Carson

Hi All,

I want to use scrJ_minus_O as the function to fill in the array "a" in
population_decomposition. But the g++ compiler always says the line "
pop_dec(scrJ_minus_O<double>(.5));" is wrong.
[snip]

template <class _Tp, class _Generator>
inline population_decomposition<_Tp, _Generator>
pop_dec(const _Generator& generator){
return population_decomposition<_Tp,_Generator>(generator);
}

main(int argc, char *argv[]){
pop_dec(scrJ_minus_O<double>(.5));
}

How is the compiler to figure out the template argument for _Tp? Try

pop_dec<double>(scrJ_minus_O<double>(.5));

(you have also omitted the return type int from main).
 
P

Peng Yu

Hi All,

I want to use scrJ_minus_O as the function to fill in the array "a" in
population_decomposition. But the g++ compiler always says the line "
pop_dec(scrJ_minus_O<double>(.5));" is wrong.
[snip]

template <class _Tp, class _Generator>
inline population_decomposition<_Tp, _Generator>
pop_dec(const _Generator& generator){
return population_decomposition<_Tp,_Generator>(generator);
}

main(int argc, char *argv[]){
pop_dec(scrJ_minus_O<double>(.5));
}

How is the compiler to figure out the template argument for _Tp? Try

pop_dec<double>(scrJ_minus_O<double>(.5));

(you have also omitted the return type int from main).
The thing that I don't understand is why we could only specify only
one type(double) for pop_dec while it has two types(_Tp, _Generator)?

Peng
 
J

John Carson

Peng Yu said:
template <class _Tp, class _Generator>
inline population_decomposition<_Tp, _Generator>
pop_dec(const _Generator& generator){
return population_decomposition<_Tp,_Generator>(generator);
}

main(int argc, char *argv[]){
pop_dec(scrJ_minus_O<double>(.5));
}

How is the compiler to figure out the template argument for _Tp? Try

pop_dec<double>(scrJ_minus_O<double>(.5));

(you have also omitted the return type int from main).

The thing that I don't understand is why we could only specify only
one type(double) for pop_dec while it has two types(_Tp, _Generator)?


You can specify both types if you want. However, you have the option to omit
those template arguments that the compiler can deduce itself from the
function's arguments. Note, however, that only trailing template arguments
can be omitted. If, say, the compiler can deduce the first argument but not
the second, then you would have to supply both (this is the same as the rule
for template arguments that have defaults --- once again, you can only omit
trailing arguments).
 
P

PengYu.UT

I'm not very sure how to specify the type of the function
object(_Generator). Would you please tell me how to specify it
explicitly in the above example?

Thanks,
Peng
 
J

John Carson

I'm not very sure how to specify the type of the function
object(_Generator). Would you please tell me how to specify it
explicitly in the above example?

pop_dec as defined in your code takes an argument of type reference to
_Generator. When you call pop_dec, you use the following code:

pop_dec(scrJ_minus_O<double>(.5));

This means that _Generator is the type of scrJ_minus_O<double>(.5).

scrJ_minus_O<double>(.5) calls the constructor for scrJ_minus_O<double> and
thus creates a temporary object of type scrJ_minus_O<double>. _Generator is
therefore scrJ_minus_O<double>.

A fully explicit call of pop_dec would be:

pop_dec<double, scrJ_minus_O<double> >(scrJ_minus_O<double>(.5));
 

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,755
Messages
2,569,535
Members
45,007
Latest member
obedient dusk

Latest Threads

Top