How can I move these global variables to be I'm taking a sample I found on the web about how to get ROS and arduino to communicated together over serial. I have the sample working and now I'm moving the idea of the sample into my OOP project and am running into some trouble.

My sketch works but when I move a certain global variable to a protected member it breaks my sketch.

How do I declare this global variable

ros::Publisher chatter("chatter", &amp;str_msg);


as a protected variable

protected:
  ros::Publisher chatter("chatter", &amp;str_msg);


without getting this error

expected identifier before string constant


sample .ino

/*
 * rosserial Publisher Example
 * Prints "hello world!"
 */

#include &lt;ros.h&gt;
#include &lt;std_msgs/String.h&gt;

ros::NodeHandle nh;

std_msgs::String str_msg;
ros::Publisher chatter("chatter", &amp;str_msg);

char hello[13] = "hello world!";

void setup()
{
  nh.initNode();
  nh.advertise(chatter);
}

void loop()
{
  str_msg.data = hello;
  chatter.publish( &amp;str_msg );
  nh.spinOnce();
  delay(1000);
}


ROSController class

#include &lt;ros.h&gt;
#include &lt;std_msgs/String.h&gt;

class ROSController {
  protected:
    ros::NodeHandle _nh;
    std_msgs::String str_msg;
//    ros::Publisher chatter("chatter", &amp;str_msg);
    int _throttle;
    bool _is_on = false;
  public:
    void loop();
    ROSController();
    double* getAttitude();
    bool isOn() {
      return this-&gt;_is_on;
    }
    int getThrottlePerc() {
      return this-&gt;_throttle / ROSController::THR_MAX;
    }

    static const int THR_MAX;
};

const int ROSController::THR_MAX = 100;

ROSController::ROSController() {
  this-&gt;_nh.initNode();
//  this-&gt;_nh.advertise(chatter);
}

double* ROSController::getAttitude() {
  return new double[3]{0, 0, 0};
}

void ROSController::loop() {
//  str_msg.data = new char[] {"hello"};
//  chatter.publish( str_msg );
  this-&gt;_nh.spinOnce();
  delay(1000);
}
The problem you are having is you are trying to both define and declare a class member variable at the same time. You can't. Instead you need to split it:

protected:
    ros::Publisher chatter;

ROSController::ROSController() : chatter("chatter", &amp;str_msg)
{
    this-&gt;_nh.initNode();
//  this-&gt;_nh.advertise(chatter);
}


That is, set up the variable as a protected member, then have the constructor call the constructor for you with the right parameters.

(I think that is the right syntax off the top of my head).