Smart way to call between classes

What is the smart way to call and create classes each other type? I am very new to C++ and I come from a very OO background in Java, and I am having troule rapping my mind around pointers. I would love if you could show me how I should write my code to do the function I want AND send me in the right way of what to do in the future.

The GameManager class is created first.

GameManager header:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
#ifndef GAMEMANAGER_H
#define GAMEMANAGER_H

#include "Renderer.h"

class GameLoop;
class GameManager
{
    public:
        GameManager();
        virtual ~GameManager();
        Renderer getRenderer();
    protected:

    private:
        Renderer renderer;
        GameLoop gameLoop;
};

#endif // GAMEMANAGER_H 


GameManager source:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
#include "GameManager.h"
#include <iostream>

using namespace std;
GameManager::GameManager()
{
    gameLoop(this);
    gameLoop.run(this);
}

GameManager::~GameManager()
{

}

Renderer GameManager::getRenderer()
{
    cout << "Test";
}


GameLoop header:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
#ifndef GAMELOOP_H
#define GAMELOOP_H

class GameManager;
class GameLoop
{

public:
    GameLoop(GameManager * gameManager);
    virtual ~GameLoop();
    void init();
    void run();

protected:

private:
    GameManager* gameManager;
    bool running = true;
};

#endif // GAMELOOP_H 


GameLoop source:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
#include "GameLoop.h"
#include <iostream>
#include "time.h"

using namespace std;

GameLoop::GameLoop(GameManager* gameManager)
{
    GameLoop::gameManager = gameManager;
}

void GameLoop::run()
{
    clock_t start;

    start = clock();
    int seconds = 1, fps = 0, updates = 0;
    do
    {
        int time = (clock() - start) / (double)(CLOCKS_PER_SEC / 1000);
        if(time > 1000)
        {
            cout << seconds;
            seconds++;
            start = clock();
            cout << "FPS: " << fps << "\n";

            fps = 0;
            updates = 0;
        }

        gameManager->getRenderer();
        fps++;
    }
    while(running);
}

GameLoop::~GameLoop()
{

}


The error I get is a "invalid use of incomplete type 'class GameManager'", as well as "forward declaration of 'Class GameManager'. I have tried all solutions that I could find, and nothing is making sense to me.

Thanks in advance.
Last edited on
closed account (48bpfSEw)
A forward declaration only says that a particular class will be defined later, so it's ok to reference it or have pointers to objects, etc. However a forward declaration does not say what members a class has, so as far as the compiler is concerned you can't use any of them until <YOUR CLASS> is fully declared.


http://stackoverflow.com/questions/20013901/im-getting-an-error-invalid-use-of-incomplete-type-class-map



Necip (that's me ^^) says : build up your #includes carefully!
Last edited on
What would I do to fix this? I kinda understand why it happens but I don't know how to work around it.
Topic archived. No new replies allowed.