Use of return_type in definition of function object

A

Alan

Am I missing some sort of declaration or is there a syntax error in
the definition of the binary function object "less_metric1" below? My
compiler says: "ISO C++ forbids declaration of `return_type' with no
type."

Thanks, Alan


#include <iostream>
#include <algorithm>
#include <functional>
#include <vector>

using namespace std;

class measurement
{
public:
double metric1, metric2, metric3;
};

class less_metric1 : binary_function<measurement, measurement, bool>
{
public:
return_type operator()(const measurement& a, const measurement& b)
{
return a.metric1 < b.metric1;
}
};

int main()
{
// Create objects
measurement data1, data2, data3;
data1.metric1 = 1; data1.metric2 = 2; data1.metric3 = 3;
data2.metric1 = 2; data2.metric2 = 3; data2.metric3 = 1;
data3.metric1 = 3; data3.metric2 = 2; data3.metric3 = 1;



system("PAUSE");
}
 
A

Alan

Never mind! "return_type" is not a keyword, so apparently I needed
the actual return type of "bool".
 
A

Alan

Then, it should compile? Changing it made it compile but not work as
intended.

Did I get the syntax wrong or something?

Thanks, Alan
 
M

Michael DOUBEZ

Alan a écrit :
Never mind! "return_type" is not a keyword, so apparently I needed
the actual return type of "bool".

"result_type" is acceptable because of binary_function<> traits:
namespace std {
template <class Arg1, class Arg2, class Result>
struct binary_function{
typedef Arg1 first_argument_type;
typedef Arg2 second_argument_type;
typedef Result result_type;
};
}
 
M

Michael DOUBEZ

Alan a écrit :
Then, it should compile? Changing it made it compile but not work as
intended.

Did I get the syntax wrong or something?

I cannot say since I don't knwwo what you mean by "not work as intended".
How did you use it ?

Michael
 
Z

Zeppe

Alan said:
Am I missing some sort of declaration or is there a syntax error in
the definition of the binary function object "less_metric1" below? My
compiler says: "ISO C++ forbids declaration of `return_type' with no
type."

sure, because the actual name for the return type is result_type, not
return_type. ;)

Regards,

Zeppe
 
O

Old Wolf

class less_metric1 : binary_function<measurement, measurement, bool>
{
public:
return_type operator()(const measurement& a, const measurement& b)
{
return a.metric1 < b.metric1;
}
};

I think you should be using public derivation here
(the default is private for 'class').
 

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,995
Messages
2,570,226
Members
46,815
Latest member
treekmostly22

Latest Threads

Top