C++ aggregation/composition not working?

I am frustrated ... assume this piece of code:

class A {
public:
  A(word x) {
    x; // dummy code
  }
};

A x(40);

// These are not relevant to show my problem, just in so you can cut&paste to try it out.
void setup() {}
void loop() {}

This works as expected: the compiler happily does what it had been made for!

Now add this to the code:

class B {
  A a(24);
};

I would expect this to be a second class definition with a single component object that is of class A and declare it through its constructor with a constant argument as I did on the global scope, where this worked.

resulting in:

class A {
public:
  A(word x) {
    x; // dummy, just to have ... something
  }
};

A x(40);

class B {
  A a(24);
};

// These are not relevant to show my problem, just in so you can cut&paste to try it out.
void setup() {}
void loop() {}

and watch the compiler complaining:

sketch_mar01a:13: error: expected identifier before '(' token
sketch_mar01a:13: error: expected ',' or '...' before numeric constant

Seems like the compiler mistakes the component declaration as a method declaration. What did I miss?

This is my first steps on Arduino (hacking for 3 days now) please help me out of that frustration, so far it was so much fun.

If at least I'd understand it?!?

Missing "public:" perhaps?

The EverHelpful® IDE doing it’s thing again by throwing obstacles in our path … (it the IDE preprocessing your PDE file, not THE preprocessor).

// File - Classes.h

#include <WProgram.h>

class A
{
public:
  A(uint8_t x);
};

class B
{
    A a;

 public:
    B();
};
// File - Classes.cpp

#include "Classes.h"

A::A(uint8_t x)
{}

B::B() : a(24)
{}
// File - ArduinoJig.pde

#include "Classes.h"

A x(40);

void setup()
{}

void loop()
{}

lloyddean:
The EverHelpful® IDE doing it's thing again by throwing obstacles in our path ... (it the IDE preprocessing your PDE file, not THE preprocessor).

Please explain how this is related to what the IDE is doing.

Your problem is that A a(24); in a class is meaningless. Are you trying to create a function that returns A, called a, and takes an int? Then you say A a(int a); Are you trying to have class B contain class A? Then you'd say A a; and construct it in your constructor's initialization list, like this:

class A {
public:
  A(word x) {
    x; // dummy, just to have ... something
  }
};

A x(40);

class B {
  A a;
public:
  B() :
  a(24)
    {
  }
};

// These are not relevant to show my problem, just in so you can cut&paste to try it out.
void setup() {}
void loop() {}

I tried your version with standard g++ and it gives the same error, so not fault with the IDE.
I tried my version and it compiles.

tl;dr: you have to construct things in the constructor.

WizenedEE,

My bad - I was compiling against an the interim version of 'Mpide' by mistake.

Other than that your solution and mine are the same with the exception of taking place across several files which is the way I prefer to work anyway.

Thanks guys,

I see the point. My mistake, last time I developed using C++ is almost 10 years ago. I get old! =(

At least I see now my problem ... me ;-). Will browse a bit in some C++ book before I ask the next stupid question ... my memory betrays me ;-).

Thanks a lot for rescueing my motivation!