getInstance()-function wont work

I am using SFML 2.1 and codeblocks. I tried to write a "getInstance()"-function for the CGame-class but anything wont work. Pls help.

CGame.cpp:

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
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
#include "CGame.hpp"

CGame::CGame()
{
}

void CGame::Init(sf::RenderWindow* x)
{
CGame::getInstance().getWindow() = x;
}

sf::RenderWindow* CGame::getWindow()
{
    return pwindow;
}

static CGame& CGame::getInstance()
{
    static CGame instance;
    return &instance;
}
//CGame.hpp:
#ifndef CGAME_HPP
#define CGAME_HPP

#include <SFML/Graphics.hpp>

class CGame
{
private:

    CGame();
    sf::RenderWindow* pwindow;

public:
    void Init(sf::RenderWindow* x);
    sf::RenderWindow* getWindow();
    static CGame& getInstance();
};

#endif // CGame


//main.cpp:
#include "CGame.hpp"

int main ()
{
    sf::RenderWindow* window = new sf::RenderWindow(sf::VideoMode(800, 600), "");

CGame game();
game.Init(window);

    while (window->isOpen())
    {

        sf::Event event;
        window->pollEvent(event);

        if (event.type == sf::Event::Closed)
            window->close();
    }
    return 0;
}


I got the following errors:
1
2
3
4
5
6
7
||=== Build: Debug in catch me (compiler: GNU GCC Compiler) ===|
PROJECTS\catch me\CGame.cpp||In member function 'void CGame::Init(sf::RenderWindow*)':|
PROJECTS\catch me\CGame.cpp|9|error: lvalue required as left operand of assignment|
PROJECTS\catch me\CGame.cpp|17|error: cannot declare member function 'static CGame& CGame::getInstance()' to have static linkage [-fpermissive]|
PROJECTS\catch me\CGame.cpp|20|error: invalid initialization of non-const reference of type 'CGame&' from an rvalue of type 'CGame*'|
PROJECTS\catch me\CGame.cpp|21|warning: control reaches end of non-void function [-Wreturn-type]|
||=== Build failed: 3 error(s), 1 warning(s) (0 minute(s), 0 second(s)) ===|

Last edited on
What happens if you remove the ampersand from line 22?
the errors in 20 and 21 are gone, thx :D but 9 and 17 still exist
well it looks like GetWindow will return you something (i dont know anything about SFML), but then you're trying to assign x to it, which looks a bit wrong.

What is GetWindow() supposed to return?
Maybe you need setWindow instead? If such a method exists that is?

Or you are supposed to be assigning the return value of getWindow to x? i.e. you need to do this:
x = CGame::getInstance().getWindow();


All guesswork on my account though sorry :)

edit: looking at how you are calling it in main() i'd go with my last suggestion.
Last edited on
I want to set the pwindow equal to x, so... it should be right,
and if i use "pwindow" instead of "getWindow()" in 9, it just error 17 exists ;)
remove the word 'static' from line 17.
you've already declared it as a static method in your header file.
Tanks man, thats it :D
No worries. Make sure you understand why your changes fixed stuff :)
Topic archived. No new replies allowed.