0

I tried to compile my code. Can you help me pls. Here's my error log : C:\Users\emile\AppData\Local\Temp\cc2GBUeM.o:main.cpp:(.text+0x104):undefined reference to `Costco::Costco()' C:\Users\someone\AppData\Local\Temp\cc2GBUeM.o:main.cpp:(.text+0x111): undefined reference to `Player::Player()' C:\Users\someone\AppData\Local\Temp\cc2GBUeM.o:main.cpp:(.text+0x19f): undefined reference to `Player::update()' C:\Users\someone\AppData\Local\Temp\cc2GBUeM.o:main.cpp:(.text+0x1b7): undefined reference to `Costco::initCostco(sf::RenderWindow&)'
C:\Users\someone\AppData\Local\Temp\cc2GBUeM.o:main.cpp:(.text+0x1d0): undefined reference to `Player::show(sf::RenderWindow&)'
collect2.exe: error: ld returned 1 exit status

Main.cpp

#include <SFML/Graphics.hpp>
#include <iostream>
#include <conio.h>
#include "include/Player.hpp"
#include "include/Costco.hpp"

constexpr int WINDOW_WIDTH{800};
constexpr int WINDOW_HEIGHT{600};

int main() {

    sf::RenderWindow window{sf::VideoMode{WINDOW_WIDTH, WINDOW_HEIGHT}, "Super Costco Fight Ultimate"};
    auto costco = Costco{};
    auto player = Player{};

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

        costco.initCostco(window);

        player.show(window);
        window.display();
    }
    std::cout<<"Press any button to quit..."<<std::endl;
    getch();
    return EXIT_SUCCESS;

}

Costco.cpp

#include "../include/Costco.hpp"
#include <iostream>

Costco::Costco()
{
    if(!texture.loadFromFile("background.png")) {
        std::cerr<<"Can't load the player texture"<<std::endl;
    }
    sprite.setTexture(texture);
}

void Costco::initCostco(sf::RenderWindow& window) {
    window.draw(sprite);
}

Costco.hpp

#ifndef COSTCO_HPP_INCLUDED
#define COSTCO_HPP_INCLUDED

#include <SFML/Graphics.hpp>

class Costco {

    public:
    Costco();
    void initCostco(sf::RenderWindow& window);

    private:
    sf::Sprite sprite;
    sf::Texture texture;

};

#endif

Player.cpp

#include "../include/Player.hpp"
#include <iostream>

Player::Player()
{
    if(!texture.loadFromFile("player.png")) {
        std::cerr<<"Can't load the player texture"<<std::endl;
    }
    sprite.setTexture(texture);
}

void Player::update() {
    if(sf::Keyboard::isKeyPressed(sf::Keyboard::Up)) {
        Player::forward();
    }
}

void Player::forward() {
    sprite.move(10, 0);
}
void Player::show(sf::RenderWindow& window) {
    window.draw(sprite);
}

Player.hpp

#ifndef PLAYER_HPP_INCLUDED
#define PLAYER_HPP_INCLUDED

#include <SFML/Graphics.hpp>

class Player {
    public:
    Player();
    void update();
    void forward();
    void show(sf::RenderWindow& window);

    private:
    sf::Texture texture;
    sf::Sprite sprite;
};

#endif

Thank's for helping me!

P.S. I use VSCode, SFML-2.5.1, I compile with g++ version 7.3.0. and here's my compilate line :

g++ c:\Users\someone\Desktop\Programmation\SuperCostcoFightUltimate\main.cpp -o SuperCostcoFightUltimate.exe -I"C:\SFML-2.5.1\include" -L"C:\SFML-2.5.1\lib" -lsfml-graphics -lsfml-window -lsfml-system

I searched like one day long and I find nothing.

Thebannedone
  • 55
  • 11
  • 1
    You need to compile and link `Costco.cpp` and `Player.cpp` too. – Fred Larson Mar 18 '20 at 13:46
  • How I'm pretty new to C++ can you explain me pls. – Thebannedone Mar 18 '20 at 13:50
  • On the command line you gave, there is nothing about `Costco` or `Player` at all. You would need to either add `Costco.cpp` and `Player.cpp` (perhaps with their full paths), or compile them separately and add the ".o" files to your command line. – Fred Larson Mar 18 '20 at 13:52

0 Answers0