Pass an instance to another class and access it c++

I'm simply passing a pointer to an instance of a class to another class but I just can't access anything from the pointer. It's really, really starting to bug me. Here's a really simple example of the code:

//manager.h
Game* game = new Game();
BClass* bc = new BClass(this);

//bclass.h
BClass(Manager* bcref) {
    bcref->game //error
    bcref->bc //error
}
//The errors are "left of ->x must point to class/struct/union/generic type.

I understand this is because I need to forward declare but adding things such as "class Manager;" at the top doesn't seem to do anything. I still can't access a member or method. I'm really trying to do something that's incredibly simple and I've been pulling my hair out for hours. I've googled and tried pretty much everything I can. Any ideas?

Answers


In order to be able to do this:

BClass* bc = new BClass(this);

Manager needs the definition of BClass. This is presumably in bclass.h. In order to do this

bcref->game;

BClass needs the definition of Manager. This is presumably in manager.h.

So you would need include each header in the other, which would lead to a cyclic include dependency. What you really need to do is break the dependency by moving the implementations to bclass.cpp and manager.cpp respectively, and use only forward declarations in your headers.


Need Your Help

Send email to multiple recipients (with recipients from textbox.value)

vba outlook outlook-vba

I am trying to create a taskmangement in Outlook with Custom Forms.