Array of functions into Library

I’m trying to implement the following code, courtesy of Nick Gammon, by creating a library:

class foo {

  public:

   void doAction0 ()
    {
      Serial.println (0);
    }

    void doAction1 ()
    {
      Serial.println (1);
    }

    void doAction2 ()
    {
      Serial.println (2);
    }

    void doAction3 ()
    {
      Serial.println (3);
    }

    void doAction4 ()
    {
      Serial.println (4);
    }

   // typedef for class function
   typedef void (foo::*GeneralFunction) ();

   // array of function pointers
    GeneralFunction doActionsArray [5] =
    {
      &foo::doAction0, 
      &foo::doAction1, 
      &foo::doAction2, 
      &foo::doAction3, 
      &foo::doAction4, 
    };

};  // end of class foo

void setup ()
{
  Serial.begin (115200);
  Serial.println ();
  Serial.println (F("Starting"));

  // test instance of foo
  foo bar;

  for (int i = 0; i < 5; i++)
    {
    // get member function pointer from array
    foo::GeneralFunction f = bar.foo::doActionsArray [i];
    // call the function
    (bar.*f) ();
    }  // end of for loop

}  // end of setup

void loop () { }

I feel like I’m close, but after creating the h and cpp files, my code does not compile. How do I break this out into the respective h and cpp files? I’ve tried several combinations but not having any luck.

W h y

S o

M u c h

W a s t e d

W h i t e

S p a c e?

foo.h:

#ifndef FOO_H
#define FOO_H
#include <Arduino.h>

class foo {
  public:
    void doAction0() {
      Serial.println(0);
    }

    void doAction1() {
      Serial.println (1);
    }

    void doAction2() {
      Serial.println (2);
    }

    void doAction3() {
      Serial.println (3);
    }

    void doAction4() {
      Serial.println (4);
    }

    // typedef for class function
    typedef void (foo::*GeneralFunction) ();

    // array of function pointers
    GeneralFunction doActionsArray [5] =
    {
      &foo::doAction0,
      &foo::doAction1,
      &foo::doAction2,
      &foo::doAction3,
      &foo::doAction4,
    };
};
#endif

.ino file:

#include "foo.h"

void setup () {
  Serial.begin (115200);
  Serial.println ();
  Serial.println (F("Starting"));

  foo bar;

  for (int i = 0; i < 5; i++) {
    // get member function pointer from array
    foo::GeneralFunction f = bar.foo::doActionsArray [i];
    // call the function
    (bar.*f) ();
  }
}

void loop () { }

No need for a .cpp file as your class is completely defined within the declaration.

Don't know why so much white space...just copied the code.

Thank you, this did compile! You are quick!

I’m trying to pass the variable “intensity” to my function call via pointer array, but having no luck. I’ve tried many combinations, but can’t get it to compile.

arrayTestarduinoForum.h (632 Bytes)

I tried posting all three files. What am I doing wrong?

arrayTestarduinoForum.cpp (594 Bytes)