views:

236

answers:

4

Firstly, I've got functions like this.

void func1();
void func2();
void func3();

Then I create my typedef for the array:

void (*FP)();

If I write a normal array of function pointers, it should be something like this:

FP array[3] = {&func1, &func2, &func3};

I want to make it a constant array, using const before "FP", but I've got this error messages:

error: cannot convert 'void ( * )()' to 'void ( * const)()' inialization

PD: Sorry my bad English.

EDIT:

x.h

typedef void (*FP)();

class x
{
 private:
  int number;
  void func1();
  void func2();
  void func3();
  static const FP array[3];
}

x.cpp

const FP x::array[3] = {&x::func1, &x::func2, &x::func3};

My code is more large and complex, this is a summary

+6  A: 

Then I create my typedef for the array: void (*FP)();

Did you miss typedef before void?

Following works on my compiler.

 void func1(){}
 void func2(){}
 void func3(){}

 typedef void (*FP)();


 int main()
 {
     const FP ar[3]= {&func1, &func2, &func3};
 }

EDIT

(after seeing your edits)

x.h

 class x;
 typedef void (x::*FP)(); // you made a mistake here

 class x
 {
   public:
      void func1();
      void func2();
      void func3();
      static const FP array[3];
 };
Prasoon Saurav
I was using a class..., it takes me the same error using const FP array[3] = ... or using FP const array[3] = ...
Facon
Show us your entire *actual* code.
Prasoon Saurav
I edited the post.
Facon
Uhh, you got an good eye, great! Given that his error message said `void (*)()`, i was thinking myself of static member functions, not checking whether they really are. Now all the question is - did he miss writing `static`, or did he typo'ed the typedef or/and did he typo'ed the error message? xD
Johannes Schaub - litb
@Johannes: Yeah, his question before the edits was a bit confusing(because the code had no errors). xD :-)
Prasoon Saurav
Now I've got this error:x.cpp:line: error: must use .* or ->* to call pointer-to-member function in 'x::array[((int)((x*)this)->x::number)] (...)'Calling the array in another x class method.How do you call it?
Facon
+1  A: 
    typedef void (*FPTR)();

FPTR const fa[] = { f1, f2};
// fa[1] = f2;  You get compilation error when uncomment this line.
Jagannath
+1  A: 

If you want the array itself to be const:

FP const a[] =
    {
        func1,
        func2,
        func3
    };
John Dibling
Whether he writes `FP const` or `const FP` makes no difference. See this question: http://stackoverflow.com/questions/1808471/is-const-lpvoid-equivalent-to-void-const
Johannes Schaub - litb
@ltib: While I agree, I'm not sure I see the relevance of your comment. Perhaps you're just observing, in which case, cheers.
John Dibling
I was just a bit confused, since your answer and the question are basically identical. Just that your's is a non-member array (and that you have the const at another place). So i'm not seeing the relevance of your answer. But maybe you are just observing likewise, so cheers.
Johannes Schaub - litb
+3  A: 

Which compiler are you using? This works on VS2005.

#include <iostream>

void func1() {std::cout << "func1" << std::endl;}
void func2() {std::cout << "func2" << std::endl;}
void func3() {std::cout << "func3" << std::endl;}

int main()
{
int ret = 0;

typedef void (*FP)();

const FP array[3] = {&func1, &func2, &func3};

return ret;
}
sand
g++ of GNU Compiler
Facon