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

problem with pointer to class method

P: n/a
Ive been searching this newsgroup for examples and help with pointers
to class methods, there were results but none solved my problem. My
problem is a compiler error.

When I compile the folowing I recieve an error:
Error: must use .* or ->* to call pointer-to-member function in `
pGetAge (...)'

The thing is i have used ->* as directed. Here is what i have

// Main:

int main(int argc, char *argv[])
{
Cat * myCat = new Cat(6);

int (Cat::*pGetAge) (void) const;
pGetAge = &Cat::GetAge;

cout << "Cat is " << myCat->*pGetAge() << " years old!" << endl;

delete myCat;

system("PAUSE");
return EXIT_SUCCESS;
}

=======================
//Class Declaration:

class Cat
{
public:
Cat(int age = 1): mAge(age) {}
~Cat(){}

int GetAge(void) const { return mAge; }
void SetAge(int age) { mAge = age; }
protected:
int mAge;
};
From what I read the syntax is corect and this should work. I am

useing gcc 3.3.1

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


P: n/a
Kelly Mandrake wrote:
When I compile the folowing I recieve an error: int main(int argc, char *argv[])
{
Cat * myCat = new Cat(6);

int (Cat::*pGetAge) (void) const;
pGetAge = &Cat::GetAge;

cout << "Cat is " << myCat->*pGetAge() << " years old!" << endl;
You need (myCat->*pGetAge)(); the function call operator bind tighter than the
pointer to member dereference operator.
delete myCat;

system("PAUSE");
return EXIT_SUCCESS;
}


Jonathan
Jul 23 '05 #2

P: n/a
That worked like clockwork. Thanks very much, I was getting fusterated.

Jul 23 '05 #3

This discussion thread is closed

Replies have been disabled for this discussion.