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

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

P: n/a
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[i] = _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));
}

Jul 23 '05 #1
Share this Question
Share on Google+
5 Replies


P: n/a
<Pe*******@gmail.com> wrote in message
news:11**********************@l41g2000cwc.googlegr oups.com
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).
--
John Carson

Jul 23 '05 #2

P: n/a
On Sat, 16 Apr 2005 14:42:49 +1000, "John Carson"
<jc****************@netspace.net.au> wrote:
<Pe*******@gmail.com> wrote in message
news:11**********************@l41g2000cwc.googleg roups.com
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
Jul 23 '05 #3

P: n/a
"Peng Yu" <pe*******@gmail.com> wrote in message
news:bv********************************@4ax.com
On Sat, 16 Apr 2005 14:42:49 +1000, "John Carson"
<jc****************@netspace.net.au> wrote:
<Pe*******@gmail.com> wrote in message
news:11**********************@l41g2000cwc.googlegr oups.com
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).

--
John Carson

Jul 23 '05 #4

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

Jul 23 '05 #5

P: n/a
<Pe*******@gmail.com> wrote in message
news:11********************@g14g2000cwa.googlegrou ps.com
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));
--
John Carson
Jul 23 '05 #6

This discussion thread is closed

Replies have been disabled for this discussion.