-2

Well,

I typedef function pointer as :

typedef string(*t_arr[5])();

and then i create function in class as :

void initStringFunctionPointer(t_arr t_err);

and then i declared as :

void Register::initStringFunctionPointer(t_arr t_err)
{
    t_err[0] = &getName;
    t_err[1] = &getSurname;
    t_err[2] = &getUsername;
    t_err[3] = &getPassword;
    t_err[4] = &getEmail;
}

but all of lines gives this error :

C:\Qt Projects\base\ClassProject_tmp\goinlib.cpp:20: error: ISO C++ forbids taking the address of an unqualified or parenthesized non-static member function to form a pointer to member function.  Say '&Register::getName' [-fpermissive]
     t_err[0] = &getName;
                 ^

I don't know what to do, Any ideas ?

Edit :

It worked, static or doing type is succeed, thank you.

1 Answers1

0

It tells you right in the error message:

Say '&Register::getName'

Once you do this you will probably discover that it's illegal anyway because you can't store a pointer-to-member-function in a regular function pointer. You'll need to change your typedef therefore, or else make getName() and the rest static. To change the typedef, try this:

typedef string(Register::*t_arr[5])();

Hopefully that's right, the array thing makes me a little uncertain; you might consider keeping the array part out of the basic typedef and adding it on elsewhere (on a second typedef or at the place of use).

John Zwinck
  • 239,568
  • 38
  • 324
  • 436
  • I already tried that,when i do that it gives me ' C:\Qt Projects\base\ClassProject_tmp\goinlib.cpp:20: error: cannot convert 'std::string (Register::*)() {aka std::basic_string (Register::*)()}' to 'std::string (*)() {aka std::basic_string (*)()}' in assignment t_err[0] = &Register::getName; ^' – Sasha Valentine Sep 13 '14 at 12:13
  • @SashaValentine Try what I said in my latest comment. – AStopher Sep 13 '14 at 12:14
  • @SashaValentine you didn't change your definition of `t_arr` – M.M Sep 13 '14 at 12:53
  • @MAttMcNabb actually i figured it out that but, i didnt figure out how to use that type in main function.I mean i want to use it like in for loop t_arr[i](); – Sasha Valentine Sep 13 '14 at 12:56
  • `t_arr` is a typedef , `t_arr[i]` makes no sense. in any case, update your question to clarify what you are asking. Also if you change it to MCVE format then it will be easier to answer – M.M Sep 13 '14 at 12:58
  • i typedefed : typedef string(Register::*t_arr[5])(); and initalized in function like : t_err[0] = &Register::getName; im doing in main function t_arr t_vec and tried that 't_vec[i]()' – Sasha Valentine Sep 13 '14 at 13:00