Why can't I initialize a member of one class as an object of another?

Hello, I've been having some troubles with classes lately. My issue is this: I want to make a class with a constructor, then make another class with a member which is an object of the first class, and I want to fill in the arguments for the constructor function when I declare this member. Here's an example of the code:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
enum func_ability{    high,   medium,   low};

class motion_control{

public:
    bool controller;
    func_ability function;
    float cost;

    motion_control(bool ctrl, func_ability func, float price){
        controller = ctrl;
        function = func;
        cost = price;
    }
};

class Xbox_Game{

private:
    bool online;
    motion_control kinect(false, high, 150.00); //HERE is where my problem is. I know I'm declaring the object properly, yet it still won't work.

};


When I try to build this code, I get the errors:

error: expected identifier before 'false'

error: expected ',' or '...' before 'false'

Is this something you just can't do in C++? Clearly those error messages don't make any sense. The compiler doesn't seem to know what the heck I'm trying to do.
Last edited on
Pretty sure you need to put the call to motion_control's constructor in Xbox_Game's constructor, not in the class declaration.
I'll try that, but it doesn't sound right. kinect is going to be a member, so how could I mention it in the constructor without declaring it first? If I declare it without any arguments, it might initialize using the compiler's default constructor, but then I couldn't give it its unique values with my own constructor. Maybe if I made a friend function that acted as a constructor, but then that's beyond the point, really. I'm mostly interested to know if it's possible to initialize an object as a member of a class, calling that object's constructor in the process.
Last edited on
This is done using the initialization list. See
http://www.cplusplus.com/forum/general/52866/

With the newest C++ standard this can be done directly like in your attempt, but compiler support isn't quite there yet.
That's exactly what I was looking for. Works beautifully. I can't believe it hasn't been in the standard longer lol
Topic archived. No new replies allowed.