0

i would like to initialize the static struct std_msgs::ColorRGBA. Unfortunately it has no constructor to initialize its 4 float members and since it is not my own class, i can not modify its constructors.

class A {
static std_msgs::ColorRGBA white; // struct with 4 float members;
};

How can i initialize the static member white? I can not use c++11

I tried

std_msgs::ColorRGBA A::white  = { 1.0f, 1.0f,  1.0f, 1.0f};

the struct looks like:

template <class T>
struct ColorRGBA_ {
  typedef ColorRGBA_<T> Type;

  ColorRGBA_(): r(0.0), g(0.0), b(0.0), a(0.0)  { }
  ColorRGBA_(const ContainerAllocator& _alloc) : r(0.0), g(0.0) , b(0.0), a(0.0)  {  }

   typedef float _r_type;
  _r_type r;
   typedef float _g_type;
  _g_type g;
   typedef float _b_type;
  _b_type b;
   typedef float _a_type;
  _a_type a;

  typedef boost::shared_ptr< ::std_msgs::ColorRGBA_<ContainerAllocator> > Ptr;
  typedef boost::shared_ptr< ::std_msgs::ColorRGBA_<ContainerAllocator> const> ConstPtr;
  boost::shared_ptr<std::map<std::string, std::string> > __connection_header;

};

best regards

3 Answers3

1

You could do an init function (e.g., ColorRGBA_init) like the example below:

#include <iostream>

namespace std_msgs {
  struct ColorRGBA {
    double r;
    double g;
    double b;
    double a;
  };    
}

std_msgs::ColorRGBA ColorRGBA_init(double const r = 1.0, double const g = 1.0, const double b = 1.0, double const a = 1.0) {
  std_msgs::ColorRGBA out;
  out.r = r;
  out.g = g;
  out.b = b;
  out.a = a;
  return out;
}

class A {
public:
static std_msgs::ColorRGBA white; // struct with 4 float members;
};

std_msgs::ColorRGBA A::white = ColorRGBA_init();

int main() {
    std::cout << A::white.r << std::endl;
}
101010
  • 41,839
  • 11
  • 94
  • 168
1

Write any init function for the type, e.g., like this:

std_msgs::ColorRGBA GenerateColorRGBA() {
    std_msgs::ColorRGBA color;
    color.r = 1.0f;
    color.g = 1.0f;
    color.b = 1.0f;
    color.a = 1.0f;
    return color;
}

std_msgs::ColorRGBA A::white = GenerateColorRGBA();
ALittleDiff
  • 1,191
  • 1
  • 7
  • 24
1

Without any knowledge of why that struct is templated or needs to be declared like that, since it's not an aggregate and you can't initializer-list initialize it, you should rather use an initializer function or method like the following

#include <iostream>
using namespace std;

namespace std_msgs {

  template <class T>
  struct ColorRGBA_ {
    typedef ColorRGBA_<T> Type;

    ColorRGBA_(): r(0.0), g(0.0), b(0.0), a(0.0)  { }
    //ColorRGBA_(const ContainerAllocator& _alloc) : r(0.0), g(0.0) , b(0.0), a(0.0)  {  }

    typedef float _r_type;
    _r_type r;
    typedef float _g_type;
    _g_type g;
    typedef float _b_type;
    _b_type b;
    typedef float _a_type;
    _a_type a;

    /*typedef boost::shared_ptr< ::std_msgs::ColorRGBA_<ContainerAllocator> > Ptr;
    typedef boost::shared_ptr< ::std_msgs::ColorRGBA_<ContainerAllocator> const> ConstPtr;
    boost::shared_ptr<std::map<std::string, std::string> > __connection_header;*/

  };

  typedef ColorRGBA_<float> ColorRGBA;

}

class A {
public:
  static std_msgs::ColorRGBA white; // struct with 4 float members;
};

// std_msgs::ColorRGBA A::white = {1.0f,1.0f,1.0f,1.0f}; // Just works for aggregates


std_msgs::ColorRGBA initializeMe() {
  std_msgs::ColorRGBA obj;
  obj.r = 22.0f;
  // ..
  return obj;
}

std_msgs::ColorRGBA A::white = initializeMe();

int main()
{
  cout << A::white.r;
}

http://ideone.com/7LOlOe

Community
  • 1
  • 1
Marco A.
  • 43,032
  • 26
  • 132
  • 246