Archived

This topic is now archived and is closed to further replies.

Yratelev

Term does not evaluate to a function

Recommended Posts

ok, heres a cut down of my prof for u guys: class CAT { int age; int weight; void (CAT::*pFunc)(); void Setagetozero() {age = 0;} } Main() { CAT pussy; pussy->pFunc = Setagetozero; pussy->pFunc(); <--- this is the lind i get the error on } y does it say that " Term does not evaluate to a function"? I have no idea how to get this to work, the function and the pointer MUST remain inside CAT. Any ideas? thanks Yratelev

Share this post


Link to post
Share on other sites
There are a great many errors in the code above. The code below works:

  
class CAT
{
public:

int age;
int weight;

void (CAT::*pFunc)();

void Setagetozero() {age = 0;}

};

int main()
{
CAT pussy;
pussy.pFunc = &CAT::Setagetozero;
(pussy.*(pussy.pFunc)) ();
return 0;
}

I don''t know how that was a cut-down of your prof, but anyway, the fixed line shows how to properly use the pointer-to-member operator.

Share this post


Link to post
Share on other sites
oki, i would have never of guessed
(pussy.*(pussy.pFunc)) (); just too many pussies (not that im complaining.
Ill have a play around with that and see if it works. Mine doesnt have the & infront of ILLNESS when assigneing a function to a pointer, cos it chucks up the

''&'' : illegal operation on bound member function expression

error, but ill see how it goes.

cheers

Yratelev

Share this post


Link to post
Share on other sites
The .* and ->* operators are unknown to a lot of people. Basically, if you have a structure, class, or union, you access a member or method with the . operator (or the -> operator if it''s a pointer to a structure, class, or union). If you have a pointer to a structure, class, or union member or method, you access it with the .* operator (or ->* operator if it''s a pointer). For example:


  
class A
{
public:
int a;
void Foo();

int A::* aIntPtr;
void (A::*aFuncPtr)();
}

A var, *varPtr = new A;

var.aIntPtr = &A::a;
varPtr->aIntPtr = &A::a;

var.aFuncPtr = &A::Foo;
varPtr->aFuncPtr = &A::Foo;

// To access A::a via A::aIntPtr:

var.*a = 6;
varPtr->*a = 7;

// To access A::Foo() via A::aFuncPtr

var.*aFuncPtr();
varPtr->*aFuncPtr();

Share this post


Link to post
Share on other sites