shared_ptr undefined reference

i am working on my Game Engine and i am getting several undefined reference errors with my code. It will probably be easier if you see the code.

Game.h
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
#ifndef GAME_H
#define GAME_H

#include <SFML\Graphics.hpp>
#include <SFML\Audio.hpp>

#include <vector>
#include <memory>

#include "Screen.h"

class Game
{
public:
    Game();

    void run();

    void processEvents();
    void handleInput();
	void update(sf::Time delta);
	void render();

	static const int width = 800;
	static const int height = 600;

	static std::shared_ptr<Screen> screen;

private:
    sf::RenderWindow window;

    static const sf::Time TimePerFrame;
};

#endif // GAME_H 



Game.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
#include "Game.h"

const sf::Time Game::TimePerFrame = sf::seconds(1.f / 10.f);

Game::Game()
: window(sf::VideoMode(Game::width, Game::height), "<game name>")
{
}



void Game::processEvents()
{
	sf::Event event;

	while (window.pollEvent(event))
	{
		if (event.type == sf::Event::Closed)
			window.close();
	}

	Game::screen->handleInput(window);
}


void Game::update(sf::Time delta)
{
	screen->update(delta);
}

void Game::render()
{
	window.clear();
	Game::screen->render(window);
	window.display();
}

void Game::run()
{
    sf::Clock clock;
	sf::Time timeSinceLastUpdate = sf::Time::Zero;

	while (window.isOpen())
	{
		sf::Time delta = clock.restart();
		timeSinceLastUpdate += delta;

		while (timeSinceLastUpdate > Game::TimePerFrame)
		{
			timeSinceLastUpdate -= TimePerFrame;
			processEvents();
			update(TimePerFrame);
		}

		render();
	}
}


Screen.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
#ifndef SCREEN_H
#define SCREEN_H

#include <SFML\Graphics.hpp>

class Screen
{
public:
	virtual void handleInput(sf::RenderWindow& window) = 0;
	virtual void update(sf::Time delta) = 0;
	virtual void render(sf::RenderWindow& window) = 0;
};

#endif // SCREEN_H 



Errors (all in Game.cpp)
obj\Debug\Media\Code\Game.o||In function `ZN4Game13processEventsEv':|
Game Engine\Media\Code\Game.cpp|22|undefined reference to `Game::screen'|

obj\Debug\Media\Code\Game.o||In function `ZN4Game6updateEN2sf4TimeE':|
Game Engine\Media\Code\Game.cpp|28|undefined reference to `Game::screen'|

obj\Debug\Media\Code\Game.o||In function `ZN4Game6renderEv':|
Game Engine\Media\Code\Game.cpp|34|undefined reference to `Game::screen'|


I don't know why this is happening. where i am learning from has got the same code and it apparently works.

For reference i am learning from here: https://github.com/jhpy1024/sfSnake
none of those seem to be relevant to my problem
Note that we are not defining the variable in line 3. We are just telling, there is a class attribute called value of type integer, but it was never created.

Note that you are not defining the variable in line 27. You are just telling, there is a class attribute called `screen' of type `std::shared_ptr<Screen>', but it was never created.

> int A::value = 42; //creating the class attribute

std::shared_ptr<Screen> Game::screen; //creating the class attribute (┬┐what value should it have?)


In order to improve that information, I would like you to tell me what parts make it confusing to relate the example with your code
thank you ne555 i defined it and it works now.
I have another shared_ptr problem and i thought it was best to ask it here without starting a new thread.

i am wanting it to go to GameScreen when i click the play button. i have the play button working but when i try to compile the following code i get an error which will follow the code.

There is more to this code but to make this post smaller i removed the irrelevant parts
Menuscreen.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
#include <SFML/Graphics.hpp>

#include <memory>

#include "GameScreen.h"
#include "MenuScreen.h"
#include "Game.h"

void MenuScreen::handleInput(sf::RenderWindow& window)
{
    if (sf::Mouse::isButtonPressed(sf::Mouse::Left) && sf::Mouse::getPosition().x >=
 playtextBounds.left && sf::Mouse::getPosition().x <= playtextBounds.width
        && sf::Mouse::getPosition().y >= playtextBounds.top && sf::Mouse::getPosition().y <= 
playtextBounds.height)
    {
        Game::screen = std::make_shared<GameScreen>();
    }
}



and i get this error
mingw\bin\..\lib\gcc\mingw32\4.7.1\include\c++\bits\shared_ptr_base.h|908|error: 
no matching function for call to 'std::__shared_ptr<Screen, (__gnu_cxx::_Lock_policy)1u>::__shared_ptr
(std::remove_reference<std::__shared_ptr<GameScreen, (__gnu_cxx::_Lock_policy)1u>&>::type)'|

note: candidates are: template<class _Alloc, class ... _Args> std::__shared_ptr::__shared_ptr
(std::_Sp_make_shared_tag, const _Alloc&, _Args&& ...)|
It looks like GameScreen does not have an empty constructor
GameScreen doesn't have a constructor

Gamescreen.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
#include "GameScreen.h"

void GameScreen::handleInput(sf::RenderWindow& window)
{
    player.handleinput();
}
void GameScreen::update(sf::Time delta, sf::RenderWindow& window)
{
    view.reset(sf::FloatRect(0, 0, window.getSize().x, window.getSize().y));
    view.setViewport(sf::FloatRect(0, 0, 1.0f, 1.0f));
    view.setCenter(player.position.x + 280, 400);
    window.setView(view);
    player.update();
}

void GameScreen::render(sf::RenderWindow& window)
{
    player.render(window);
}
Last edited on
i was able to fix the problem by studying the code i am learning from
Last edited on
Registered users can post here. Sign in or register to post.