Serial object as a member of a class

I’m not sure if this is a bug, but the mbed cloud compiler doesn’t like this code because the Serial object is a member of the class.

class device {
	public: 
		void send();
	private:
		Serial serial(p9, p10);
};

Is there a workaround for this? Every other C++ compiler I’ve used supports putting objects inside of classes, so I don’t see why the mbed's should be any different.

Edit: Here are the errors it gives when it hits the line Serial serial(p9, p10);:

  • constant “p9” is not a type name
  • constant “p9” is not a type name

And here are the errors it gives when you try to access serial from a member function of the class device; for example, serial.printf(“Hello”);:

  • nonstandard form for taking the address of a member function
  • expression must have a class type
16 Oct 2011

Hi AVHSRC,

Having an object as a member of a class is fine in C++. But when you want to pass a member class constructor some arguments, you don't initialise it quite like you've written (on any c++ compiler); you need to use an initialisation list:

class device {
	public: 
		void send();
                device() : serial(p9, p10) {}
	private:
		Serial serial;
};

This is true whether the member items are just plain variables or classes. For more background, have a read of:

Hope that gets you moving again!

Simon