Redefinition of Constructor - c++

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.

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);

Remove trailings {} and the link to the super class ctor in the declaration file for the constructor, ie :
class Entity {
public:
Entity(int x, int y, sf::RenderWindow& win);
void tick();
With {} and : window(win) it is no more a declaration but a definition, thus you will have two definitions: one in the header and one in the source...
Remove one of them.

Related

C++ - Circular dependency between 3 Classes

I have three classes, 2 of which rely on the functionality of each other, and one which contains pointers to and is included in both:
Player:
#pragma once
#include <SFML\Graphics.hpp>
#include "RenderableObject.h"
//#include "Renderer.h"
class Renderer;
class Player : public RenderableObject
{
public:
Player(int size, Renderer* renderer);
~Player();
void render();
void setX(int x);
void setY(int y);
int getX();
int getY();
void setVelX(float x);
void setVelY(float y);
float getVelx();
float getVely();
int getSize();
private:
int size;
int x;
int y;
float velx;
float vely;
Renderer* ren;
};
GameManager:
#pragma once
#include "Player.h"
class Renderer;
class GameManager
{
public:
GameManager(Renderer* r);
~GameManager();
Player* getBall();
private:
Player* ball;
};
Renderer:
#pragma once
#include <SFML\Graphics.hpp>
#include "GameManager.h"
#include "Player.h"
class Renderer
{
public:
Renderer(GameManager* game);
~Renderer();
sf::RenderWindow* getWindow();
void draw();
void renderCircle(int size, int x, int y);
void renderSquare(int size, int x, int y);
private:
sf::RenderWindow* win;
GameManager* game;
Player* ball;
};
I suspect there is some sort of circular dependency going on here. I managed to reduce the 60 odd errors down to 3, by adding "class Renderer;" to the Player header file. However, I'm now receiving "Use of undefined type" errors.
It might help to give a higher level view of what I want to achieve:
The GameManager object should hold an instance (maybe more) of player. This GameManager object is shared between subsystems such as Renderer so they all have access to the same resources.
The Renderer object should be able to call the Player object's (retrieved from the GameManager object) render() function, which in turn should call back to the Renderer object's renderCircle() object.
I'm hoping this is a simple case of re-arranging the includes, without having to re-design what I've already done.
In a header file, where you only declare variables that are pointers or references, all the compiler needs to know is that a class exists, nothing more. It doesn't need to know how big it it, or what members the class have. Therefore it's enough with a forward declaration.
However, in the source files where the variable is used, objects are created, and member functions are called, then the compiler need the full definition which means you have to include your whole header file.
Taking your code, simplified:
Player.h
#pragma once
class Renderer;
class Player
{
public:
Player(Renderer*);
private:
Renderer* ren;
};
GameManager.h
#pragma once
class Renderer;
class GameManager
{
public:
GameManager(Renderer*);
private:
Renderer* ren;
};
Renderer.h:
#pragma once
class Player;
class GameManager;
class Renderer
{
public:
Renderer(GameManager*);
private:
GameManager* game;
Player* ball;
};
The above header files are about the same that you already have, with the exception that I used forward declarations in Renderer.h instead.
Now the Renderer.cpp source file, where the Player and GameManager objects are actually created and used, we need the full definition from the header files:
#include "Renderer.h"
#include "Player.h"
#include "GameManager.h"
Renderer::Renderer(GameManager* game)
: game(game), ball(new Player)
{
}
// Other functions that calls member function in the `man` and `ball` objects
Of course, you need to #include "Renderer.h" in the Player.cpp and GameManager.cpp source files.
If you have other errors, they are because of other things you done, not because of the circular dependencies, as those have been solved with the forward declarations.

QPixmap Image doesnt appear

Im new with Qt and im making a game in it. I want to display icon but after putting there some code i cant find it anywhere. Im confused because i dont get any errors, so im pretty sure that i just didnt add something. Help.
Combat.h file
#ifndef COMBAT
#define COMBAT
#include <QImage>
#include <QGraphicsPixmapItem>
class Combat: public QGraphicsPixmapItem{
public:
// constructors
//Combat(QPixmap *parent=NULL);
Combat(const QString x);
// setters/getters
void getOwner(QString x);
private:
QString owner;
};
Combat.cpp
#include "Combat.h"
#include <QGraphicsScene>
Combat::Combat(const QString x){
// draw graphics
setPixmap(QPixmap(x));
}
void Combat::getOwner(QString x){
owner = x;
}
#endif // COMBAT
Interface.h
#ifndef INTERFACE
#define INTERFACE
#include <QList>
#include "Hex.h"
#include "Combat.h"
class Interface{
public:
// constructors
Interface();
// getters/setters
QList<Hex*> getHexes();
void getOwner(int x);
// public methods
void placeHexes();
void placeCombat();
private:
void createHexColumn(int x, int y, int numOfRows);
QList<Hex*> hexes;
void createCombatIcon(int x, int y, QString z);
int owner;
};
#endif // INTERFACE
Interface.cpp
#include "Interface.h"
#include "Game.h"
extern Game* game;
Interface::Interface(){
}
QList<Hex *> Interface::getHexes(){
return hexes;
}
void Interface::placeHexes(){
createHexColumn(100,100,6);
}
void Interface::placeCombat()
{
createCombatIcon(100,100,":/grafika/atak_magiczny.png");
}
void Interface::createCombatIcon(int x, int y, QString z)
{
Combat* icon = new Combat(z);
icon->getOwner("player1");
icon->setPos(x,y);
game->scene->addItem(icon);
}
Im not sure where something is missing
I solved the problem by adding my graphics to resource file in Qt Creator...

inheritance in my sdl c++ pong game

So I am trying to implement a pong game using sdl but with classes and inheritance.
So I plan to have a base Puck class which defines my paddle. PlayerPuck and EnemyPuck will inherit this base class. The base Puck class handles all the initializations and drawing of the base paddle. From this I am trying to derive a subclass to add extra things on my playerPaddle like checking collisions and bounds etc. But when I try to create objects polymorphically in my main class, it throws me tons of errors.
Here is my Base Puck Header file
#include <SDL.h>
#include <SDL_image.h>
#include <SDL_ttf.h>
class Puck{
private:
SDL_Rect puckPaddle;
public:
Puck();
Puck(int x,int y,int width,int height);
int getX();
int getY();
int getHeight();
int getWidth();
SDL_Rect* getPaddleRect();
void setX(int x);
void setY(int y);
void setHeight(int height);
void setWidth(int width);
void Render(); // should be virtual, leave it for now
void Update(); // should be virtual, leave it for now
};
This is just my header file. I have done the implementation in the cpp file already and it works fine.
Now the problem is in my PlayerPuck which I have derived from Puck as follows
#include "Puck.h"
class PlayerPuck : public Puck {
public:
PlayerPuck();
PlayerPuck(int x, int y, int width,int height);
void UpdatePosition();
void CheckBounds();
};
In my main function when I do the following it gives me error like
C2504: 'Puck' base class undefined
error C2440: '=' : cannot convert from 'PlayerPuck *' to 'Puck *'
#include "Ball.h"
#include "Puck.h"
#include "PlayerPuck.h"
Puck* p;
void Initialize(){
if(SDL_Init(SDL_INIT_EVERYTHING) == -1)
running = false;
TTF_Init();
p = new PlayerPuck(50,150,200,100);
}
What is my mistake?
I solved the problem by just adding "PlayerPuck.h" header file and removing the "Puck.h" as there were two same headers in my main function
Now, I am getting linker errors if I try calling virtual functions
in my base Puck class I have defined a virtual function Test()
#include <SDL.h>
#include <SDL_image.h>
#include <SDL_ttf.h>
class Puck{
private:
SDL_Rect puckPaddle;
public:
Puck();
Puck(int x,int y,int width,int height);
int getX();
int getY();
int getHeight();
int getWidth();
SDL_Rect* getPaddleRect();
void setX(int x);
void setY(int y);
void setHeight(int height);
void setWidth(int width);
**virtual void Test();** --> here
};
And I have called it in my derived playerPuck class
#include "Puck.h"
class PlayerPuck : public Puck {
public:
PlayerPuck();
PlayerPuck(int x, int y, int width,int height);
void UpdatePosition();
void CheckBounds();
**void Test();** --> here
};
When I create my playerPuck obj in main and try calling that function as below it throws me
LNK2001 - unresolved external symbol error
#include "Ball.h"
#include "PlayerPuck.h"
PlayerPuck* playerPuck;
playerPuck = new PlayerPuck(50,200,20,100);
void Update(){
//playerPuck->UpdatePosition();
playerPuck->Test();
}

Undefined reference, using SFML

I have begun learning SFML at the same time as C++ and do not understand the rules of using & and * in RenderWindow case. Could you help me?
Main class:
#include <SFML/Graphics.hpp>
#include "Square.h"
int main()
{
sf::RenderWindow window(sf::VideoMode(200, 200), "SFML works!");
Square sq(5,5);
while (window.isOpen())
{
sf::Event event;
while (window.pollEvent(event))
{
if (event.type == sf::Event::Closed)
window.close();
}
window.clear();
sq.draw(&window);
window.display();
}
return 0;
}
Square header:
#ifndef SQUARE_H
#define SQUARE_H
class Square
{
private:
sf::RenderWindow* window;
sf::RectangleShape rectangle;
int y;
int x;
public:
Square(int coordX, int coordY);
Square();
void draw(const sf::RenderWindow* target);
};
#endif
Square class:
#include <SFML/Graphics.hpp>
class Square{
sf::RectangleShape rectangle;
int y;
int x;
public:
Square(int coordX, int coordY)
: rectangle(), y(coordY),x(coordX)
{
rectangle.setSize(sf::Vector2f(10,100));
rectangle.setOrigin(5,50);
}
Square()
: rectangle(), y(5),x(5)
{
rectangle.setSize(sf::Vector2f(10,100));
rectangle.setOrigin(5,50);
}
void draw(sf::RenderWindow* target)
{
target->draw(rectangle);
}
I can't draw a square on RenderWindow:
main.cpp:(.text+0x17d): undefined reference to `Square::Square(int, int)'
main.cpp:(.text+0x211): undefined reference to `Square::draw(sf::RenderWindow const*)'
How can I make this work?
In C++, you have function declaration and function definition:
// declaration, typically in X.h
#pragma once // don't include this twice, my dear compiler
class X {
public:
void foo();
};
Implementation:
// X.cpp
#include "X.h"
void X::foo() { ...code .... }
// here you wrote
// class X { void foo(){} };
// which is a _declaration_ of class X
// with an _inline definition_ of foo.
Usage:
// main.cpp
#include "X.h"
Applying this pattern to your code should ensure that
every symbol is defined
no symbol is defined more than once
Remove square.cpp and change your square.h file to the following. Make sure you take back-up before changing your files.
#ifndef SQUARE_H
#define SQUARE_H
class Square
{
private:
sf::RenderWindow* window;
sf::RectangleShape rectangle;
int y;
int x;
public:
Square(int coordX, int coordY)
: rectangle(), y(coordY),x(coordX)
{
rectangle.setSize(sf::Vector2f(10,100));
rectangle.setOrigin(5,50);
}
Square()
: rectangle(), y(5),x(5)
{
rectangle.setSize(sf::Vector2f(10,100));
rectangle.setOrigin(5,50);
}
void draw(sf::RenderWindow* target)
{
target->draw(rectangle);
}
};
#endif

c++ error C2065 : undeclared identifier [duplicate]

This question already has an answer here:
Closed 10 years ago.
Possible Duplicate:
C++ Undeclared Identifier (but it is declared?)
Im getting the error sprite.h(20): error C2065: 'Component' : undeclared identifier when I try to compile (I got a couple other files as well). Below is the sprite.h file. I cant for the life of me figure out what is causing this problem.
#ifndef SPRITE_H
#define SPRITE_H
#include "Image.h"
#include "Rectangle.h"
#include <string>
#include <SDL.h>
#include <vector>
#include "Component.h"
namespace GE2D {
class Sprite {
public:
Sprite();
Sprite(Image *i);
Sprite(Image *i, int x, int y);
Sprite(char *file, bool transparentBg, int x, int y, int w, int h);
virtual ~Sprite();
virtual void tick(SDL_Surface *screen, std::vector<Sprite*>* sprites, std::vector<Component*>* components);
virtual void handleEvent(SDL_Event eve);
virtual void draw(SDL_Surface *screen);
void setPosition(int x, int y);
const Rectangle& getRect() const;
const Image& getImage() const;
const Sprite& operator=(const Sprite& other);
Sprite(const Sprite& other);
protected:
private:
Image image;
Rectangle rect;
};
}
#endif
In the .cpp file tick() is defined like this:
void Sprite::tick(SDL_Surface *screen, std::vector<Sprite*>* sprites, std::vector<Component*>* components) {}
tick() is supposed to take two vectors like they do now, but maybe there's a better way to do that which might solve this problem?
EDIT
As requested, here is Component.h as well:
#ifndef COMPONENT_H
#define COMPONENT_H
#include "Rectangle.h"
#include "Component.h"
#include "Sprite.h"
#include <vector>
#include <SDL.h>
namespace GE2D {
class Component {
public:
Component();
virtual ~Component();
virtual void draw(SDL_Surface *screen) = 0;
virtual void tick(SDL_Surface *screen, std::vector<Sprite*>* sprites, std::vector<Component*>* components) = 0;
virtual void handleEvent(SDL_Event eve) = 0;
const Rectangle& getRect() const;
protected:
Component(int x, int y, int w, int h);
private:
Rectangle rect;
};
}
#endif
Sprite.h includes Component.h which includes Sprite.h, giving a circular dependency which can't be resolved.
Luckily, you don't need to include the headers at all. Each class only refers to a pointer to the other class, and for that a simple declaration is enough:
class Component;