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

4
  • LOL, got so frustrated I forgot to add the error. Commented Nov 24, 2012 at 5:29
  • 4
    Be careful, your error message and your code seem unrelated. There's no GraphicsManager function or class in your code. Commented Nov 24, 2012 at 5: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. Commented Nov 24, 2012 at 5: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. Commented Nov 24, 2012 at 11:38

1 Answer 1

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) { }
};
Sign up to request clarification or add additional context in comments.

10 Comments

@billz, I can't see why not, can you elaborate ?
I have 3 classes I am creating
@billz I'm fairly certain this is what they're looking for.
@FreakinaBox Go look up how initialization lists work, you can initialize multiple objects with them. I'll add to the answer, though.
@billz that part of the question (the error message) wasn't there till later on.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.