pointer to member function (gcc)

N

Newsgroup - Ann

I have the following code:

class nn_base {
public:
double phi(double v)
{
return v;
};
double ComputeY(int i, int j, double (nn_base::*activeFunc)(double))
{
return (this->*activeFunc)(1.0);
}
};

main()
{
nn_base nn1;
double x = nn1.ComputeY(1, 2, nn_base::phi);
return 0;
}

It can be compiled by Visual C++ both 6.0 and .NET but cannot by gcc 3.22,
which complains:


test7.cpp: In function `int main()':
test7.cpp:18: no matching function for call to `nn_base::ComputeY(int, int,
<unknown type>)'
test7.cpp:10: candidates are: double nn_base::ComputeY(int, int, double
(nn_base::*)(double))

what's wrong with that?
 
G

Gianni Mariani

Newsgroup said:
I have the following code:

class nn_base {
public:
double phi(double v)
{
return v;
};
double ComputeY(int i, int j, double (nn_base::*activeFunc)(double))
{
return (this->*activeFunc)(1.0);
}
};

main()

should be

int main()
{
nn_base nn1;
double x = nn1.ComputeY(1, 2, nn_base::phi);

should be:

double x = nn1.ComputeY(1, 2, & nn_base::phi);

actually, I'm not sure about needing the "&" but it seems like it makes
more sense if it is needed.

VC++ likes the "&".
 
N

Noah Roberts

Gianni said:
should be

int main()


In C any function that is declaired without a return type is defaulted
to return int. Therefore "main() {...}" is acceptable (though commonly
thought of as bad form) in C. Is this different in C++?

NR
 
J

Jack Klein

In C any function that is declaired without a return type is defaulted
to return int. Therefore "main() {...}" is acceptable (though commonly
thought of as bad form) in C. Is this different in C++?

NR

Implicit int is illegal in C++, and it has been illegal in C since the
1999 major update to the C language standard.

--
Jack Klein
Home: http://JK-Technology.Com
FAQs for
comp.lang.c http://www.eskimo.com/~scs/C-faq/top.html
comp.lang.c++ http://www.parashift.com/c++-faq-lite/
alt.comp.lang.learn.c-c++ ftp://snurse-l.org/pub/acllc-c++/faq
 
W

White Wolf

Noah said:
In C any function that is declaired without a return type is defaulted
to return int. Therefore "main() {...}" is acceptable (though
commonly thought of as bad form) in C. Is this different in C++?

Yes. And as far as I know in C as well. C (today) is C99 and C99 does not
allow implicit int anymore. So main withhout a return type is illegal in
both C and C++.
 
K

Kevin Goodsell

Gianni said:
should be:

double x = nn1.ComputeY(1, 2, & nn_base::phi);

actually, I'm not sure about needing the "&" but it seems like it makes
more sense if it is needed.

I believe it is needed in the case of a member function, but not in the
case of a non-member function.

-Kevin
 
N

Newsgroup - Ann

Kevin Goodsell said:
I believe it is needed in the case of a member function, but not in the
case of a non-member function.

-Kevin

Good, thank you all. Could anybody tell me why is the difference? Thanks.

-Ann
 
G

Gianni Mariani

Noah said:
In C any function that is declaired without a return type is defaulted
to return int. Therefore "main() {...}" is acceptable (though commonly
thought of as bad form) in C. Is this different in C++?

This is different in C++.

Only constructors and destructors may be declared without return types.

void is a valid return type indicating that nothing may be returned.
 

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

No members online now.

Forum statistics

Threads
474,141
Messages
2,570,818
Members
47,367
Latest member
mahdiharooniir

Latest Threads

Top