473,395 Members | 1,393 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

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

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
5 1478
<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
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
"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
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
<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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: Phil Powell | last post by:
if (is_array($_POST)) { foreach ($this->getAssocSectionsObjArray($key, $dbAP) as $obj) { print_r($obj); print_r(" in array? "); print_r(in_array($obj, $result)); print_r("<P>"); if...
5
by: The Roys | last post by:
Hi Im doing something wrong in quitting the Word.Application in my VB program. I have General Declarations Dim AppWord As Word.Application Form_Load() Set AppWord =...
7
by: Jonathan Fine | last post by:
Giudo has suggested adding optional static typing to Python. (I hope suggested is the correct word.) http://www.artima.com/weblogs/viewpost.jsp?thread=85551 An example of the syntax he proposes...
5
by: xuatla | last post by:
Hi, I encountered the following compile error of c++ and hope to get your help. test2.cpp: In member function `CTest CTest::operator+=(CTest&)': test2.cpp:79: error: no match for 'operator='...
5
by: Tim Eliot | last post by:
Just wondering if anyone has hit the following issue and how you might have sorted it out. I am using the command: DoCmd.TransferText acExportMerge, , stDataSource, stFileName, True after...
2
by: Ernst Berg | last post by:
Thankfully there is a C language group! Hello! I am a hobyist who is rediscovering writing C programs on Linux. I use GCC and I am at a loss as to why the compile fails to find the...
2
by: ANaiveProgrammer | last post by:
Hi all I have been trying to send a struct over socket. I tried to assign the values of struct to unsigned char array. I have attached both .c files at the bottom. 'Client.c' takes a struct,...
18
by: dfetrow410 | last post by:
Anyone have some code that will do this? Dave
22
by: KitKat | last post by:
I need to get this to go to each folders: Cam 1, Cam 2, Cam 4, Cam 6, Cam 7, and Cam 8. Well it does that but it also needs to change the file name to the same folder where the file is being...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.