0

I'm trying to understand how function pointer works can't clarified why I get the following err.

#include <iostream>

using namespace std;
enum COLOR {RED,BLACK,WHITE};

class Car
{public:
 Car (COLOR c): color(c) { cout<<"Car's constructor..."<<endl; CarsNum++;}
 ~Car () {cout<<"Car's destructor..."<<endl; CarsNum--;}
 void GetColor () { cout<<"Color of the car is"<<color<<endl;}
 static int GetCarsNum () {cout<<"Static membet CarsNum="<<CarsNum<<endl; return 0;}
private:
COLOR color;
static int CarsNum;

};

int Car::CarsNum=0;

int main()
{
int (Car::*pfunc) () = NULL;
pfunc=&Car::GetCarsNum ();


 Car *ptr= new Car(RED);
 ptr->GetColor ();
 ptr->GetCarsNum ();
 delete ptr;
 ptr=0;
 Car::GetCarsNum();
    return 0;
}

Err msg:

main.cpp|23|error: lvalue required as unary '&' operand

Problem is with:

  pfunc=&Car::GetCarsNum ();

Any help would be greatly appreciated

2
  • Please always indicate the exact line the error message mentions. Commented May 5, 2014 at 14:12
  • Using auto would greatly simplify this code. Commented May 5, 2014 at 14:52

4 Answers 4

2

With &Car::GetCarsNum () you are calling GetCastNum, and taking the return value to make it a pointer (with the address-of operator &).

To solve this simply drop the parentheses:

pfunc=&Car::GetCarsNum;
Sign up to request clarification or add additional context in comments.

4 Comments

@AlexanderBrevig As the member function is static then yes.
@Deduplicator: The answer does have &, though.
In this case I got another err: |23|error: cannot convert 'int()' to 'int (Car::*)()' in assignment|
@user3604569 Then you need to drop the Car:: from the declaration of pfunc. It's because the GetCarsNum is a static member function.
1

No need for parentness:

pfunc=&Car::GetCarsNum;

and

int (Car::*pfunc) () = NULL;

Oh, UPDATE: you have static method: In this case GetCarsNum() is just a simple function:

int (*pfunc) () = &Car::GetCarsNum;

4 Comments

Why not mention such small answer in comments
Because then this question will be without answer:)
I would rephrase to make it clear that not only is there no need for parentheses, the parentheses are actually wrong. (Which is why he was getting the error.)
@kec, Tejas Patel sorry for my quick, unclear answer
0

I think that by putting braces behind the method, you call the method. This confuses the compiler. who now thinks the & operator is used as binary operator. So remove the () and only specify the function name.

Comments

0

Thanks, guys. Now I see the diffrence between pointers to static method (in this case we use syntaxis as for simple function)

int (*pfunc) () = &Car::GetCarsNum;

int this case both with '&' and without '&' gives the same result.(and what is the difference). and calling it:

pfunc();

And other side - function pointer on standart method:

void (Car::*pfunc2) () = NULL;
pfunc2=&Car::GetColor;

and calling it:

(ptr->*pfunc2)();

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.