0

I have a class with two different class objects as members. I want to initialize one of the members with another, but so far I can't seem to get this to work.

For example, Game game(&input); contains an error that there must be a type specified. How might I go about doing this?

class KApp { 
private:
    Input input;
    Game game(&input);

};


class Input {
    Input() {};
};

class Game {
private:
    Input* input;
public:
    Game(Input & inp) : input(&inp) {}
    Game(Input* inp) : input(inp) {}
};
jruizaranguren
  • 12,679
  • 7
  • 55
  • 73
Chemistpp
  • 2,006
  • 2
  • 28
  • 48

1 Answers1

1

Try something like this

class KApp { 
private:
    Input input;
    Game game;
    KApp() : game(&input) {}
};

What is going on with Game game(&input); is that you're declaring a member game which the compiler has picked up as a function declaration and is complaining that the parameter doesn't have a type.

This way, you're explicitly calling Game::Game(Input*) in the constructor of KApp (at which point input should be initialized using its default constructor).

edit - Input* vs Input&

Xeren Narcy
  • 875
  • 5
  • 15
  • Prefect. Thanks man. I see what you're saying. I am trying to do it to soon. I should do it when the class object constructor is called not when the class object is declared. I made the adjustment and it worked. – Chemistpp Jul 22 '15 at 00:17