c++ error C2065 : undeclared identifier [duplicate] - c++

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;

Related

cannot declare variable " to be of abstract type "

I have a base class called "SHAPE" that only contains pure virtual functions where each method = 0.
The derived class of that base class is TRIANGLE. All of it's methods are derived from the base class SHAPE. I have closely inspected the code but I see no errors in the code. Why does the compiler keep on screaming at me that I cannot declare a variable that has a type TRIANGLE?
SHAPE.h
#ifndef SHAPE_H
#define SHAPE_H
#include "POINT.h"
class SHAPE
{
public:
SHAPE();
~SHAPE();
virtual bool isValid() const = 0;
virtual double getArea() const = 0;
virtual void set_sides() = 0;
virtual POINT operator[](const int index) const = 0;
virtual bool isInside(const POINT& other) const = 0;
};
#endif // SHAPE_H
TRIANGLE.h
#ifndef TRIANGLE_H
#define TRIANGLE_H
#include <SHAPE.h>
#include <stdio.h>
#include <iostream>
using namespace std;
class TRIANGLE : public SHAPE
{
public:
TRIANGLE();
~TRIANGLE();
TRIANGLE(const POINT* arr);
TRIANGLE(const POINT& first, const POINT& second, const POINT& third);
bool isValid() const;
double getArea() const;
void set_sides();
bool isInside() const;
POINT operator[](const int index) const;
private:
POINT *points = new POINT[3];
double sides[3];
};
#endif // TRIANGLE_H
main.cpp
#include <iostream>
#include <stdio.h>
#include "Render.h"
#include "GAME_SYSTEM.h"
#include "POINT.H"
#include "TRIANGLE.h"
#include "QUAD.h"
using namespace std;
Render* Render::RENDER = new Render();// ignore this
int main(int argc, char* argv[])
{
Render* RENDER = Render::getInstance();//ignore this
POINT a(3,3);
POINT b(3,4);
POINT c(4,3);
TRIANGLE bob(a,b,c);//The problem is here
printf("%i", bob[0].get('x'));//print the x value of the first point of triangle bob.
return 0;
}

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.
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.

missing type specifier - int assumed Error after declaration of static member

I get the following error message after compiling my code:
Error C4430 missing type specifier - int assumed. Note: C++ does not
support default-int on line 21
I dont really know why the AssetManager retrieves that error, since I included everything needed...
My code:
game.hpp
#pragma once
#include "SDL.h"
#include "SDL_image.h"
#include <iostream>
#include <vector>
#include "AssetManager.hpp"
class Game {
public:
Game();
~Game();
void init(const char* title, int xpos, int ypos, int width, int height, bool fullscreen);
void handleEvents();
void update();
bool running() { return isRunning; };
void render();
void clean();
static AssetManager* assets;
////////
}
AssetManager.hpp
#pragma once
#include <map>
#include <string>
#include "TextureManager.hpp"
#include "Vector2D.hpp"
#include "ECS.hpp"
class AssetManager {
public:
AssetManager(Manager* man);
~AssetManager();
void AddTexture(std::string id, const char* path);
SDL_Texture* GetTexture(std::string id);
private:
Manager* manager;
std::map<std::string, SDL_Texture*> textures;
};
I think this is a problem of circular dependency. Try forward declaration for AssetManager something like this:-
Class AssetManager; instead of #include "AssetManager.hpp" in game.hpp

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