Note that there are some explanatory texts on larger screens.

plurals
  1. POC++ - How to call creator class/object
    primarykey
    data
    text
    <p>I need to call properties and functions of an object from a different class.</p> <p>The idea is passing 'this' as a parameter to the other class constructor. E.g.:</p> <pre><code>instance = ClassName(this); </code></pre> <p>And then do:</p> <pre><code>ParentClass parentInstance; ClassName::ClassName(MainApp _instance){ parentInstance = _instance; } </code></pre> <p>However, my compiler says that ParentClass does not name a type. Ideas? Also, should I use a pointer to save memory? How?</p> <p>Thanks in advance.</p> <hr> <p><strong>UPDATE:</strong></p> <p>Ok, sorry for the delay. Here it goes the actual code. First, a simple class.</p> <p>Game class:</p> <p>Header file</p> <pre><code>#ifndef _GAME #define _GAME #include "ofMain.h" class Game{ public: Game(); ~Game(); void hi(); }; #endif </code></pre> <p>cpp file:</p> <pre><code>#include "Game.h" Game::Game(){} Game::~Game(){} void Game::hi(){ cout &lt;&lt; "hi, I'm game! " &lt;&lt; endl; } </code></pre> <p>Then, from MainApp I create the object: - Relevant code on header file:</p> <pre><code>#ifndef _MAIN_APP #define _MAIN_APP #include "ofMain.h" #include "Game.h" class MainApp : public ofSimpleApp{ public: Game game; }; #endif </code></pre> <p>Relevant code on the cpp file:</p> <pre><code>game = Game(); game.hi(); </code></pre> <p>This obviously works as I'm only creating a bloody object. However, problem comes with composition.</p> <p>I could pass the main app as argument in the constructor, I could pass it via game.setParent(this);... problem is, I can't even define the variable to store the reference to the app.</p> <p>E.g.: (making it easy/inefficient without pointers or anything)</p> <p>Game.h:</p> <pre><code>#define _GAME #ifndef _GAME #include "ofMain.h" #include "MainApp.h" class Game{ MainApp app; public: Game(); ~Game(); void hi(); }; #endif </code></pre> <p>This returns a "does not name a type" error and declaring <code>class MainApp</code> returns an "incomplete type" error</p> <p>I'm sure I'm doing something dumb.</p> <hr> <p><strong>UPDATE 2:</strong></p> <p>The problem with that method is that I can't call a function of the pointed object now.</p> <p>This is Game.h:</p> <pre><code>#ifndef _GAME #define _GAME #include "ofMain.h" class MainApp; class Game{ public: Game(); Game(MainApp* _app); ~Game(); void hi(); MainApp* app; }; #endif </code></pre> <p>As you see, app (of the type MainApp) is passed as a parameter. That's fine, MainApp exists as it's the forward declaration. However, when I try to call any of app's functions I can't (compiler error saying Request for member appHi in .... which is non-class type 'MainApp'.</p> <p>MainApp is NOT included in Game.h but Game.h IS included in MainApp.h.</p> <p>Ideas?</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload