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();
}
Related
I'm a beginner to C++ following this tutorial. I'm working on the section dealing with Classes and running into the following error.
not a class or struct name C/C++(262)
I am attempting to inherit the properties of a Shape parent class into a Circle class, but my editor claims that the Shape class is not defined. The error is raised on line 4 in Circle.h: class Circle: public Shape. I've included my code below.
I've double checked my code against the code in the tutorial and ensured that all the files I've written are in the same directory, etc.
Any pointers would be much appreciated.
The code editor I'm using is VSCode 1.55.2.
Code
Shape.h
#ifndef SHAPE_H
#define SHAPE_H
class Shape {
protected:
double height;
double width;
static int numOfShapes;
public:
// Constructors
Shape(double length);
Shape(double height, double width);
Shape();
virtual ~Shape();
void SetHeight(double height);
double GetHeight();
void SetWidth(double width);
double GetWidth();
static int GetNumOfShapes();
virtual double Area();
};
#endif /* SHAPE_H */
Shape.cpp
#include "Shape.h"
// This file is known as the "class implementation" file.
Shape::Shape(double length) {
// '->' is known as the pointer operator (used to access objects fields and methods).
this->height = length;
this->width = length;
}
Shape::Shape(double height, double width) {
this->height = height;
this->width = width;
}
Shape::~Shape() = default; // Sets the deconstructor to the default.
void Shape::SetHeight(double height) {
// You should really use isDigit() on each character to validate the input
this->height = height;
}
double Shape::GetHeight() {return height;}
void Shape::SetWidth(double width)
{
// You should really use isDigit() on each character to validate the input
this->width= width;
}
double Shape::GetWidth() {return width;}
int Shape::GetNumOfShapes() {return numOfShapes;}
double Shape::Area(){
return height * width;
}
int Shape::numOfShapes = 0; // Sets the default value for 'numOfShapes'
Circle.h
#ifndef CIRCLE_H
#define CIRCLE_H
class Circle: public Shape {
public:
// Constructors
Circle();
Circle(const Circle& orig);
Circle(double width);
virtual ~Circle();
double Area();
};
#endif /* CIRCLE_H */
Circle.cpp
#include "Circle.h"
#include "Shape.h"
#include <cmath>
Circle::Circle(double width): Shape(width) {}
Circle::~Circle() = default;
double Circle::Area() {
return 3.14159 * pow((width/2), 2);
}
main.cpp
#include <cstdlib>
#include <iostream>
#include "Shape.h"
#include "Circle.h"
using namespace std;
void ShowArea(Shape& shape);
int main(int argc, char **argv) {
Shape square(10, 5);
Circle circle(10);
ShowArea(square);
ShowArea(circle);
return 0;
}
void ShowArea(Shape& shape) {
cout << "Area : " << shape.Area() << endl;
}
You are including "Shape.h" after "Circle.h". Hence Circle class is defined before Shape class. The compiler when it reaches line "class Circle: public Shape", it cannot identify "Shape" as any user-defined class or struct, thus it is raising the error. To correct it include "Shape.h" before "Circle.h" or include "Shape.h" in "Circle.h"
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.
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.
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...
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;