Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Redefinition of Constructor

I have only just started learning C++ yesterday, so im completely new, although I came over from Java. I am making a game as my first small project. I want to have my player class separate from the main file. I have my header file and cpp file made up like this:

Header:

#pragma once

#include <SFML/Graphics.hpp>
#include <iostream>

class Entity {
    public:
        Entity(int x, int y, sf::RenderWindow& win) : window(win) {}
        void tick();
        void render();
    private:
        int x;
        int y;
        float velX;
        float velY;
        sf::RenderWindow& window;
        bool keys[264] = {};
};

C++:

#include <SFML/Graphics.hpp>
#include <iostream>

#include "headers/Entity.h"

using namespace sf;

Texture texture;
Sprite sprite;

Entity::Entity(int x, int y, RenderWindow& win) : window(win) {
    this->x = x;
    this->y = y;
    this->velX = 0;
    this->velY = 0;
    this->window = win;


    texture.loadFromFile("../res/img/test.png");
    sprite(texture);
}

void Entity::tick() {

    // movement
}

void Entity::render() {
    this->window.draw(sprite);
}

But in the cpp file the constructor keeps saying:

redefinition of 'Entity::Entity(int, int, sf::RenderWindow&)'

I have searched up many times how to fix this but I can't find anything that works, because this issue carries on no matter what I try. I would prefer the theory aswell as I am brand new with any answer. Thank You.

like image 244
tygzy Avatar asked Jan 19 '26 05:01

tygzy


1 Answers

Your header is providing a implementation / definition of your constructor:

    Entity(int x, int y, sf::RenderWindow& win) : window(win) {}

And so is your source file.

Change the header to just contain the declaration:

    Entity(int x, int y, sf::RenderWindow& win);
like image 81
Jesper Juhl Avatar answered Jan 20 '26 19:01

Jesper Juhl



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!