SFML Help undefined reference and #include help

Hello. I'm trying to make a game with SFML. The problem is that when I'm trying to display the sprite of the ship it is giving me an undefined reference. Also, I want to know if I can do something better than putting all of my #includes into each header, because I know that can cause problems later on.

player.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
36
37
38
39
40
41
42
43
44
45
46
47
#ifndef PLAYER_H
#define PLAYER_H
#include <SFML/Graphics.hpp>
#include <SFML/Audio.hpp>
#include <string>
#include <game.h>
#include <projectile.h>

extern projectile laser;

class player
{
    public:
        player(std::string texDirectory);

        int getLives(){return lives;}
        sf::Texture getShipTex(){return shipTex;}
        bool getIsAlive(){return isAlive;}
        int getSpeed(){return speed;}
        projectile getWeapon(){return *weapon;}
        int getHealth(){return health;}
        int getAmmo(){return ammo;}
        sf::Sprite getShipSprite (){return shipSprite;}


        void setLives(int s_Lives){lives = s_Lives;}
        void setShipTex(std::string s_shipTex){shipTex.loadFromFile(s_shipTex);}
        void setIsAlive(bool s_isAlive){isAlive = s_isAlive;}
        void setSpeed(int s_speed){speed = s_speed;}
        void setHealth(int s_health){health = s_health;}
        void setAmmo(int s_ammo){ammo = s_ammo;}

    protected:
    private:
        int lives = 3;
        sf::Texture shipTex;
        bool isAlive = true;
        int speed = 1;
        projectile *weapon = &laser;
        int health = 10;
        int ammo;
        sf::Sprite shipSprite;

};

#endif // PLAYER_H


main.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
#include <SFML/Graphics.hpp>
#include <SFML/Audio.hpp>
#include <string>
#include <projectile.h>
#include <player.h>

int main()
{


sf::RenderWindow window(sf::VideoMode(1920, 1080), "Space Assault");

sf::Texture background;
background.loadFromFile("textures/space.png");
sf::Sprite backgroundSprite;
backgroundSprite.setTexture(background);

player player("textures/ships/fighter1.png");

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

        }

        window.clear();
        window.draw(backgroundSprite);
        window.draw(player.getShipSprite());
        window.display();
    }
    return 0;
}


the error:
 
undefined refernece to 'shipSprite'

What line? What file?
includes into each header

Make an Abstract class with virtual functions.

Also, please copy paste your errors always. And tell us what you thing the problem might be.
Last edited on
An undefined reference means the linker can find no definition of a function or variable used in your program. Ensure that you have such a definition and that it is being exposed to your toolchain.
Topic archived. No new replies allowed.