Function Pointers to access a function outside a class

Hi there,

first time poster. I was pretty confident programming in C but C++ and it's classes are still a bit new to me.

I'm trying to build a class that acts as a statemachine. I want to be able to make multiple instances of it so i can have multiple statemachines running at the same time.

The states are functions that i would like to pass to that class and that will get executed with performState(). I've got an idea how it should work but I just can't get it to work.

My header file:

1
2
3
4
5
6
7
8
9
10
11
 class StateMachine
    {
       public:
          
          void setStateFunction( void (*fptr)(void ));
          void performState( void );
          StateMachine();   
     
       private:
          void (*stateFunction)(void);
    };


my cpp function:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
// ------------------------------------------------------------
// Member functions definitions including constructor
// ------------------------------------------------------------
StateMachine::StateMachine(  )
{
    StateMachine::stateFunction = NULL;
}

// ------------------------------------------------------------
// Set Function Pointer
// ------------------------------------------------------------
void StateMachine::setStateFunction( void (*fptr)(void ))
{
    StateMachine::stateFunction = fptr;
}

// ------------------------------------------------------------
// Actual state machine - call this in your program
// ------------------------------------------------------------
void StateMachine::performState( void )
{
    if( StateMachine::stateFunction != NULL )
    {
        StateMachine::stateFunction();
    }
}


in main.cpp i want to be able to do something like this:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
StateMachine myStateMachine1;


void testFunction(void)
{
    ledState3 = LOW;
}


myStateMachine1.setStateFunction( &testFunction );

while(1)
{
  myStateMachine1.performState();
}


It doesn't compile and I'm pretty sure I have the function pointers all wrong. Can I even point to functions outside the class?

Any help would be appreciated!

Thanks
Last edited on
It compiles fine for me. What error are you getting?
You code compiles fine for me, too. (in C++ Shell)

Can I even point to functions outside the class?

Yes, you can.

Andy

PS Your code looks a little unusual to me with the class scope being used inside the methods. (I've also lost the C-ish void function parameters.)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
#include <iostream>

// FROM HEADER FILE

  typedef void (*PFNSTATEFUNCTION)(); // I prefer to use typedef

  class StateMachine
    {
      public:
        void setStateFunction(PFNSTATEFUNCTION fptr);
        void performState();
        StateMachine();   
     
      private:
        PFNSTATEFUNCTION stateFunction;
    };

// FROM CPP FILE

// ------------------------------------------------------------
// Member functions definitions including constructor
// ------------------------------------------------------------
StateMachine::StateMachine() : stateFunction(NULL) // use nullptr in C++11 code
{
    //stateFunction = NULL; moved to init list
}

// ------------------------------------------------------------
// Set Function Pointer
// ------------------------------------------------------------
void StateMachine::setStateFunction(PFNSTATEFUNCTION fptr)
{
    stateFunction = fptr;
}

// ------------------------------------------------------------
// Actual state machine - call this in your program
// ------------------------------------------------------------
void StateMachine::performState()
{
    if( stateFunction != NULL )
    {
        //stateFunction();
        (*stateFunction)(); // overkill,  but reminds me it's a function pointer
    }
}

//StateMachine myStateMachine1; moved into main()

void testFunction()
{
    //ledState3 = LOW;
    std::cout << "ledState3 = LOW\n";
}

int main()
{
    StateMachine myStateMachine1; // prefer non-global

    myStateMachine1.setStateFunction( testFunction ); // the & is optional in &testFunction

    //while(1)
    for(int i = 0; i < 3; ++i) // FOR TESTING
    {
      myStateMachine1.performState();
    }

    return 0;
}
Last edited on
great thank you guys - legends!

it compiled in the end. I'm working for the first time with the Spark Development board and didn't realise all code needs to go in the void loop(void) section.

thank you andy westken for the syntax corrections ... i've taken it all on board for the future.

Cheers
Topic archived. No new replies allowed.