1

Been banging my head against this all day with many trips to google. I have a master object that needs to create several other objects in its constructor the main object gets variables in its constructor that are passed on to the objects it creates.

class WorldManager{
  public:
  WorldManager(int x, int y, int z){
    //do stuff
  }
}

class GameManager{
  public:
  WorldManager world;
  GameManager(int x, int y, int z){
    world(x,y,z);
  }
}

I get error

error: no matching function for call to `GAMEMANAGER::GraphicsManager(HWND__*&, int&, int&)'

it works untill I ask for args in the constructors of the world class

FreakinaBox
  • 421
  • 1
  • 4
  • 15
  • LOL, got so frustrated I forgot to add the error. – FreakinaBox Nov 24 '12 at 05:29
  • 4
    Be careful, your error message and your code seem unrelated. There's no `GraphicsManager` function or class in your code. – J.N. Nov 24 '12 at 05:37
  • Thinking in terms of "manager" objects and, in general, dominance relationships between parts of the code, will probably cause you worse headaches than this little technicality. – Cheers and hth. - Alf Nov 24 '12 at 05:42
  • Move all the creation code into a Factory. Pass a Configuration object to the Factory. Inside the Factory, assemble your object graph from the inside out, passing the config data only to the components that need them. Do not push the config data through the object graph. – Gordon Nov 24 '12 at 11:38

1 Answers1

3

I think that you want:

class GameManager{
public:
    WorldManager world;
    GameManager(int x, int y, int z) : world(x, y, z) { }
};

The weird colon thing is called an initialization list, and it does construction of member objects and parent classes and a bunch of other things.


If you have more than one object that you want to construct, add them to the list:

class GameManager{
public:
    WorldManager world1, world2;
    GameManager(int x, int y, int z) : world1(x, y, z), world2(x, y, z) { }
};
juanchopanza
  • 223,364
  • 34
  • 402
  • 480
Xymostech
  • 9,710
  • 3
  • 34
  • 44