7

I'm trying to deal with an array of functions, however when I assign the functions to the array (in the class's default constructor) I am greeted with the error message:

"void (GameObject::*)()" cannot be used to initialize an entity of type "AlarmFunction""

All code dealing with this is as follows, this is all in the header file:

#include "stdafx.h"
#include <Windows.h>

typedef int (*AlarmFunction) ();

class GameObject
{
protected:
GameObject()
{
    AlarmFunction alarmF[12] =
    {
        AlarmEvent1,
        AlarmEvent2,
        AlarmEvent3,
        AlarmEvent4,
        AlarmEvent5,
        AlarmEvent6,
        AlarmEvent7,
        AlarmEvent8,
        AlarmEvent9,
        AlarmEvent10,
        AlarmEvent11,
        AlarmEvent12
    };
}
//private default constructor
~GameObject();
int instance_id;
int object_id;
int alarm[12];
void AlarmEvent1();
void AlarmEvent2();
void AlarmEvent3();
void AlarmEvent4();
void AlarmEvent5();
void AlarmEvent6();
void AlarmEvent7();
void AlarmEvent8();
void AlarmEvent9();
void AlarmEvent10();
void AlarmEvent11();
void AlarmEvent12();
AlarmFunction alarmF[12];

public:
void AlarmTick()
{
    for (int i=0;i<=11;i++)
    {
        if (alarm[i] > -1)
        {
            alarm[i]--;
        }
        else
        {
            if (alarm[i] == 0)
            {
                alarmF[i]();
            }
        }
    }
}

I can't find much on the web about this error or indeed how to fix it, and would be grateful if anyone could shed some light on the error for me.

1
  • Are you sure those 12 events couldn't be condensed into one function? Commented May 2, 2013 at 1:55

3 Answers 3

8

That's because you're trying to assign a pointer to member function to a field of type pointer to free function. They're incompatible types.

I'm not sure of the exact syntax since I haven't had to deal with raw pointers to member functions (std::function is better for most cases anyway), but you could try something like this to see if it works.

class GameObject;
typedef int (GameObject::*AlarmFunction) ();
Sign up to request clarification or add additional context in comments.

3 Comments

I don't fully understand but is there a way to make this work? I paraphrased this from a tutorial I found so I assumed it worked.
@Adam member functions have an implicit this parameter, so they're not compatible with non-member functions. Hence the need for extra decoration in the typedef.
Ah that seemed to clear up the problem, many thanks :) However as soon as I fixed that I got a new error simply telling me: "array of functions is not allowed"
4

I don't know much about c++ but I also ran into the same problem as you while coding in C and instead of passing the name of functions to an array of functions I stored in the array the addresses of the functions and then I use an array pointer to call the functions

int test1()
{
  printf("hello\n");
  return 0;
}

int test2()
{
  printf("world\n");
  return 1;
}

int main()
{
  int n = -15;

  int (*(functions[2]))() = {test1, test2};

  n = functions[0]();

  printf("%d\n", n);
  return 0;
}

Comments

1

Like @Antimony said, the types are incompatible.

What you could do is something similar to this:

Instead of declaring void functions

void AlarmEvent1();
void AlarmEvent2();
void AlarmEvent3();
void AlarmEvent4();
void AlarmEvent5();
void AlarmEvent6();
void AlarmEvent7();
void AlarmEvent8();
void AlarmEvent9();
void AlarmEvent10();
void AlarmEvent11();
void AlarmEvent12();

You can declare them as follow:

int AlarmEvent1() {return 1};
int AlarmEvent2() {return 1};
int AlarmEvent3() {return 1};
int AlarmEvent4() {return 1};
//So on

This way, you can add them to your array and use them.

I haven't tried to compile it yet, but it should work or at least the direction for you is there.

Please correct me if I'm wrong.

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.