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

Callback member functions

P: n/a
Hi, I have two methods A::Conv1 and A::Conv2 that transform coordinates
in two different ways. A::Draw() should take the pointer to one
of these methods as the parameter, and call it.

MSVC++ compiler reports: "term does not evaluate to a function taking
4 arguments". If the CONV type is not a pointer to a member function
taking 4 arguments, then what a hell it is? :)
GCC reports:
"must use .* or ->* to call pointer-to-member function in conv",
which doesn't helps, too.

How should I call the conv function?

class A {
public:
void Conv1(double X, double Y, int& x, int& y);
void Conv2(double X, double Y, int& x, int& y);
typedef void (A::*CONV)(double, double, int&, int&);
void Draw(CONV conv);
void ReDraw();
A() {
_xscale = 0.5, _yscale = 0.5, _xshift = 100, _yshift = 200;
}
private:
double _xscale, _yscale;
int _xshift, _yshift;
};

void A::Conv1(double X, double Y, int& x, int& y) {
x = (int)(X * _xscale), y = (int)(Y * _yscale);
}
void A::Conv2(double X, double Y, int& x, int& y) {
x = (int)(X * _xscale) + _xshift, y = (int)(Y * _yscale) + _yshift;
}
void A::Draw(CONV conv) {
int x, y;
conv(0.5, 0.5, x, y); // error C2064: term does not evaluate to a
function
}

void A::ReDraw() {
Draw(Conv1);
}

int main() {
A a;
a.ReDraw();
return 0;
}

Jun 23 '06 #1
Share this Question
Share on Google+
5 Replies


P: n/a
ka*******@gmail.com wrote:
[..]
void A::Draw(CONV conv) {
int x, y;
conv(0.5, 0.5, x, y); // error C2064: term does not evaluate to a
function
The syntax to call a member function through a pointer to member is

(this->*conv)(0.5, 0.5, x, y);

What book are you reading that doesn't explain that?
}

void A::ReDraw() {
Draw(Conv1);
}

int main() {
A a;
a.ReDraw();
return 0;
}


V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
Jun 23 '06 #2

P: n/a
I've come up with workaround using friend functions. It does exactly
what I want, but the syntax looks terrible. If someone knows how
to solve the initial problem, I will be very glad to hear you.

#include <stdio.h>
class A;
typedef void (*CONV)(A*, double, double, int&, int&);

class A {
public:
void Draw(CONV conv);
void ReDraw();
A() {
_xscale = 5.0, _yscale = 5.0, _xshift = 100, _yshift = 200;
}
friend void Conv1(A* a, double X, double Y, int& x, int& y);
friend void Conv2(A* a, double X, double Y, int& x, int& y);
private:
double _xscale, _yscale;
int _xshift, _yshift;
};

void Conv1(A* a, double X, double Y, int& x, int& y) {
x = (int)(X * a->_xscale),
y = (int)(Y * a->_yscale);
}
void Conv2(A* a, double X, double Y, int& x, int& y) {
x = (int)(X * a->_xscale) + a->_xshift,
y = (int)(Y * a->_yscale) + a->_yshift;
}
void A::Draw(CONV conv) {
int x, y;
conv(this, 0.5, 0.5, x, y); // error C2064: term does not evaluate to
a function
printf("x=%d y=%d\n", x, y);
}

void A::ReDraw() {
printf("Conv1:\n");
Draw(Conv1);
printf("Conv2:\n");
Draw(Conv2);
}

int main() {
A a;
a.ReDraw();
return 0;
}

Jun 23 '06 #3

P: n/a
* ka*******@gmail.com:
Hi, I have two methods A::Conv1 and A::Conv2 that transform coordinates
in two different ways. A::Draw() should take the pointer to one
of these methods as the parameter, and call it.

MSVC++ compiler reports: "term does not evaluate to a function taking
4 arguments". If the CONV type is not a pointer to a member function
taking 4 arguments, then what a hell it is? :)
GCC reports:
"must use .* or ->* to call pointer-to-member function in conv",
which doesn't helps, too.
Why doesn't it help?

It tells you exactly what's /technically/ wrong, and what you must do to
fix it /technically/.

How should I call the conv function?


See the GCC error message.

--
A: Because it messes up the order in which people normally read text.
Q: Why is it such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?
Jun 23 '06 #4

P: n/a
kankowski wrote:
Hi, I have two methods A::Conv1 and A::Conv2 that transform coordinates
in two different ways. A::Draw() should take the pointer to one
of these methods as the parameter, and call it.


http://www.parashift.com/c++-faq-lit...o-members.html

Always check the FAQ before posting! Read 33.2.

--
Phlip
http://c2.com/cgi/wiki?ZeekLand <-- NOT a blog!!!
Jun 23 '06 #5

P: n/a
Thank you, it works now. Many thanks!

Jun 23 '06 #6

This discussion thread is closed

Replies have been disabled for this discussion.