In library TOADO.h
#ifndef _TOADO_H
#define _TOADO_H
class TOADO
{
public:
TOADO();
void SoDiem(int soluong);
void addToaDo(POINT *toado);
void setWidth(int width);
public:
int SoLuong;
int Width;
POINT* ToaDo;
};
#endif
In library FIGURE.h
#include "TOADO.h"
#ifndef _FIGURE_H
#define _FIGURE_H
class FIGURE:public TOADO
{
public :
FIGURE();
virtual void Draw(Graphics &graphics);
};
#endif
IN LINE.h
#include "FIGURE.h"
#ifndef _LINE_H
#define _LINE_H
class LINE:public FIGURE
{
public:
LINE(const Color &clr,POINT* line );
void Draw(Graphics &graphics);
private:
TOADO Line;
Color color;
};
#endif
int main()
{
LINE A(color, point); // char color , point is array point
A.setWidth(2);// function setWidth() in class TOADO
}
why i recall base function A.setWidth(2) but function A.Width() still return Width = 1, it don't change Width = 2;
everyone can explain for me and instruct for me how to fix it .
thanks.
so sorry, my english very bad, i don't can't describe to everyone understand
The problem is function setWidth().
it's my code
https://www.mediafire.com/?28dzv62x1kvxci2
Related
App.h:
#ifndef APP_H
#define APP_H
#include <SFML/Graphics.hpp>
#include <vector>
#include "RoomManager.h"
typedef unsigned int uint;
class App {
private:
sf::RenderWindow window;
sf::Event evt;
uint screen_width = 0;
uint screen_height = 0;
sf::Clock deltaClock;
float deltaTime = 0.0f;
RoomManager& roomManager = RoomManager::GetInstance();
std::vector<sf::Sprite> allDrawables;
App() { }
~App() { }
public:
static App& GetInstance() {
static App instance;
return instance;
}
void CreateWindow(uint width, uint height) {
screen_width = width;
screen_height = height;
window.create(sf::VideoMode(screen_width, screen_height), "Point'n'click adventure");
}
void Update() {
while (window.isOpen()) {
deltaTime = deltaClock.restart().asSeconds();
while (window.pollEvent(evt)) {
if (evt.type == sf::Event::EventType::Closed)
window.close();
}
Draw();
}
}
void ResizeScreen(uint width, uint height) {
screen_width = width;
screen_height = height;
window.setSize(sf::Vector2u(screen_width, screen_height));
}
void AddDrawable(sf::Sprite& sprite) {
allDrawables.push_back(sprite);
}
void Draw() {
window.clear();
for (int i = 0; i < allDrawables.size(); i++) {
window.draw(allDrawables[i]);
}
window.display();
}
};
#endif
DrawableEntity.h:
#ifndef DRAWABLEENTITY_H
#define DRAWABLEENTITY_H
#include <SFML/Graphics.hpp>
#include "App.h"
class DrawableEntity {
private:
sf::Texture backgroundTexture;
sf::Sprite backgroundSprite;
public:
DrawableEntity() {
App::GetInstance().AddDrawable(backgroundSprite);
}
~DrawableEntity() { }
void LoadTexture(const std::string texturePath) {
backgroundTexture.loadFromFile(texturePath);
backgroundSprite.setTexture(backgroundTexture);
}
};
#endif
RoomManager.h:
#ifndef ROOMMANAGER_H
#define ROOMMANAGER_H
#include <SFML/Graphics.hpp>
#include <vector>
#include "Room.h"
class RoomManager {
private:
std::vector<Room> rooms;
public:
static RoomManager& GetInstance() {
static RoomManager instance;
return instance;
}
void AddRoom(Room room) {
rooms.push_back(room);
}
};
#endif
Room.h:
#ifndef ROOM_H
#define ROOM_H
#include <SFML/Graphics.hpp>
#include "App.h"
#include "DrawableEntity.h"
class Room {
private:
DrawableEntity background;
public:
Room(const std::string backgroundTexturePath) {
background.LoadTexture(backgroundTexturePath);
}
~Room() { }
};
#endif
main.cpp:
#include <SFML/Graphics.hpp>
#include "App.h"
using namespace sf;
int main() {
App& app = App::GetInstance();
RoomManager& roomManager = RoomManager::GetInstance();
app.CreateWindow(1920, 1080);
roomManager.AddRoom(Room("room_0_background.jpg"));
app.Update();
return 0;
}
My idea here is that whenever I would create DrawableEntity object, it would automatically add its sprite to allDrawables vector in App. The above code throws me C2653 error saying that "'App' is not a class or namespace name". The error appears in DrawableEntity() constructor, at line where I call AddDrawable() method. It's been a while since I coded in C++ (now I code mostly in C#) and I'm not sure how to fix this.
[Note: As, you haven't shared DrawableEntity() constructor code(I didn't find any) I am assuming your problem might be the following. If the following solution does not solve your issue, please share DrawableEntity() constructor code.]
You must have called AddDrawable() as following:
App& appInstance = App::GetInstance();
appInstance::AddDrawable(); // it is wrong
You shouldn't call like above
Rather do the following:
App& appInstance = App::GetInstance();
appInstance.AddDrawable(); // it is correct
Edit(Possible solution, this must be happening in your code):
I guess #drescherjm is right. Please, take a careful look at the code snippets below. I have tested some codes similiar like your code structure:
so_test.cpp file:
#include <cstdio>
#include "so_header.h"
int main() {
A& objA = A::getInstance();
int xa = objA.getX();
printf("xa: %d\n", xa);
B& objB = B::getInstance();
int xb = objB.getX();
printf("xb: %d\n", xb);
return 0;
}
so_header.h file:
#ifndef CLASS_A
#define CLASS_A
#include <cstdio>
#include "so_header_2.h"
class A {
private:
int x = 100;
A(){}
public:
static A& getInstance() {
static A obj;
return obj;
}
int getX() {
return x;
}
void print() {
B& objB = B::getInstance();
int xb = objB.getX();
printf("xb: %d\n", xb);
}
};
#endif
so_header_2.h:
#ifndef CLASS_B
#define CLASS_B
#include <cstdio>
#include "so_header.h"
class B {
private:
int x = 200;
B(){}
public:
static B& getInstance() {
static B obj;
return obj;
}
int getX() {
return x;
}
void print() {
A& objA = A::getInstance();
int xa = objA.getX();
printf("xa: %d\n", xa);
}
};
#endif
When I compile so_test.cpp, I get the follwowing error(using mingw compiler):
In file included from so_header.h:6:0,
from so_test.cpp:2:
so_header_2.h: In member function 'void B::print()':
so_header_2.h:21:3: error: 'A' was not declared in this scope
A& objA = A::getInstance();
^
so_header_2.h:21:6: error: 'objA' was not declared in this scope
A& objA = A::getInstance();
^
so_header_2.h:21:13: error: 'A' is not a class, namespace, or enumeration
A& objA = A::getInstance();
The reason is in print function of A, A is calling B and in print function of B, B is calling A.
Your code structure follows a similiar pattern. Let's start from DrawableEntity.
> DrawableEntity calls App
> App calls RoomManager
> RoomManager calls Room
> Room calls App and DrawableEntity(notice, the order here. App is called before DrawableEntity, and so loop has come back to App)
> App calls RoomManager...and ...so on...
So, the loop is:
DrawableEntity -> App -> RoomManager -> Room -> App -> RoomManager -> ...
As, App is the start of the loop or the first object in the loop to be called, you're getting error recognizing "App" class...cause, you started declaring App, but you've never finished declaring it, and in the middle of declaring it, you are trying to use it. So, you're basically trying to use something that hasn't even seen the light of existence(An analogy could be: its still in womb, not born yet...).
This has happended with your code.
To solve your problem, you need to reorganize your code structure a bit. Just break the cycle(i.e. loop) of call dependency, and you'd be okay.
I'm facing a problem using forward declaration, and I don't know how to fix it. Here's my files:
BubblePlug.h
#ifndef _BUBBLEPLUG_
#define _BUBBLEPLUG_
#include "IPlug_include_in_plug_hdr.h"
#include "resource.h"
#include "IControl.h"
class IPianoRoll;
class IMidiEngine;
class BubblePlug: public IPlug
{
private:
public:
IMidiEngine *pMidiEngine;
IPianoRoll *pPianoRoll;
BubblePlug(IPlugInstanceInfo instanceInfo);
~BubblePlug();
};
#endif // !_BUBBLEPLUG_
BubblePlug.cpp
#include "BubblePlug.h"
#include "IPlug_include_in_plug_src.h"
#include "IPianoRoll.h"
#include "IMidiEngine.h"
BubblePlug::BubblePlug(IPlugInstanceInfo instanceInfo) : IPLUG_CTOR(10, 1, instanceInfo) {
pPianoRoll = new IPianoRoll(this, 8, 8);
pMidiEngine = new IMidiEngine(this);
}
BubblePlug::~BubblePlug() {
delete pPianoRoll;
delete pMidiEngine;
}
IPianoRoll.h
#ifndef _IPIANOROLL_
#define _IPIANOROLL_
#include "IMidiEngine.h"
class IPianoRoll : public IControl
{
private:
BubblePlug *pBubblePlug;
public:
IPianoRoll(BubblePlug *bubbleplug, int x, int y) : IControl(bubbleplug, IRECT(x, y, x + 10, y + 10)), pBubblePlug(bubbleplug) {
}
~IPianoRoll() {
};
bool Draw(IGraphics *pGraphics) {
return true;
}
void Random(bool onlyScore = false) {
pBubblePlug->pMidiEngine->Init();
}
void Start() {
}
};
#endif // !_IPIANOROLL_
IMidiEngine.h
#ifndef _IMIDIENGINE_
#define _IMIDIENGINE_
class IMidiEngine
{
private:
BubblePlug *pBubblePlug;
public:
IMidiEngine(BubblePlug *bubbleplug) : pBubblePlug(bubbleplug) {
}
~IMidiEngine() {
};
void Init(bool randomScore = true) {
pSamplwhk->pPianoRoll->Start();
}
};
#endif // !_IMIDIENGINE_
when I compile, it says around pSamplwhk->pPianoRoll->Start();:
use of undefined type 'IPianoRoll'
left of '->Start' must point to class/struct/union/generic type
VS2015 find each element writing the code (I've no problem), it happens only when I compile (Build).
Why? I pass BubblePlug and I do forward of both IPianoRoll and IMidiEngine, including them in order (on BubblePlug.cpp).
IMidiEngine should know everythings about IPianoRoll (which it is included first).
At least, I should have problem at "runtime", why at compile?
Can you help me to understand the problem and how to fix it? Thanks.
IPianoRoll.h includes IMidiEngine.h, so no matter in which order you include the two files, the definition of IPianoRoll will always come after the init function where it is being used.
One way to avoid this is to move the body of the init function into a separate .cpp file:
In IMidiEngine.h:
void Init(bool randomScore=true);
In IMidiEngine.cpp:
void IMidiEngine::Init(bool randomScore) {
pSamplwhk->pPianoRoll->Start();
}
I have 4 files car.cpp, car.h, motorvehicle.h and vehicle.h. I am programming in the QT-creator environment.
The issue i am having is:
error: out-of-line definition of 'getSafetyRating' does not match any declaration in 'vehicle::Car'
int Car::getSafetyRating()
Here are the program files for reference, i am new to learning c++ and would really appreciate the help! Cheers Alex. I Apologies in advance for any repost of problems if any.
vehicle.h
#ifndef VEHICLE_H
#define VEHICLE_H
#include <string>
namespace vehicle
{
class Vehicle
{
public:
Vehicle(int numberOfPassengers,
int topSpeed,
int numberOfWheels,
std::string color = "red");
virtual ~Vehicle();
virtual std::string getColor();
virtual int getTopSpeed();
virtual int getNumberOfWheels();
virtual int getNumberOfPassengers();
virtual int getSafetyRating() = 0;
protected:
int m_numberOfPassengers;
int m_topSpeed;
int m_numberOfWheels;
std::string m_color;
};
}
#endif // VEHICLE_H
motorvehicle.h
#ifndef MOTORVEHICLE_H
#define MOTORVEHICLE_H
#include "vehicle.h"
namespace vehicle
{
class MotorVehicle : public Vehicle
{
public:
MotorVehicle(int numberOfPassengers,
int topSpeed,
int numberOfWheels,
double kilometresPerLitre);
MotorVehicle(int numberOfPassengers,
int topSpeed,
int numberOfWheels,
std::string color,
double kilometresPerLitre);
virtual ~MotorVehicle();
virtual double getKilometresPerLitre();
protected:
double m_kmpl;
};
}
#endif // MOTORVEHICLE_H
car.h
#ifndef CAR_H
#define CAR_H
#include "motorvehicle.h"
namespace vehicle
{
class Car : public MotorVehicle
{
public:
Car(int numberOfPassengers,
int topSpeed,
double kilometresPerLitre,
int numberOfAirBags = 2,
bool abs = true,
int numberOfWheels = 4);
Car(int numberOfPassengers,
int topSpeed,
double kilometresPerLitre,
std::string color,
int numberOfAirBags = 2,
bool abs = true,
int numberOfWheels = 4);
virtual ~Car();
virtual int getNumberOfAirBags();
virtual bool hasAutomaticBreakingSystem();
protected:
int m_numberOfAirBags;
int m_abs;
};
}
#endif // CAR_H
car.cpp
#include "car.h"
using namespace vehicle;
Car::Car(int numberOfPassengers,
int topSpeed,
double kilometresPerLitre,
int numberOfAirBags,
bool abs,
int numberOfWheels) :
MotorVehicle(numberOfPassengers, topSpeed, numberOfWheels, kilometresPerLitre),
m_numberOfAirBags(numberOfAirBags),
m_abs(abs)
{
}
Car::Car(int numberOfPassengers,
int topSpeed,
double kilometresPerLitre,
std::string color,
int numberOfAirBags,
bool abs,
int numberOfWheels):
MotorVehicle(numberOfPassengers, topSpeed, numberOfWheels,color, kilometresPerLitre),
m_numberOfAirBags(numberOfAirBags),
m_abs(abs)
{
}
Car::~Car()
{
}
int Car::getNumberOfAirBags()
{
return m_numberOfAirBags;
}
bool Car::hasAutomaticBreakingSystem()
{
return m_abs;
}
int Car::getSafetyRating()
{
int SafetyRating = 0;
if (m_numberOfAirBags >= 4)
{
SafetyRating += 3;
}
else if (m_numberOfAirBags >= 2)
{
SafetyRating += 2;
}
else if (m_numberOfAirBags > 0)
{
SafetyRating += 1;
}
if (m_abs)
{
SafetyRating += 2;
}
return SafetyRating;
}
Your car class does not have a getSafetyRating declared in the .h file, and the pure virtual function in vehicle requires it. When you declare something pure virtual, ie func() = 0, you basically say that any class that inherits from it MUST implement this function.
So both the motor vehicle class, and the car class must at the very least declare the getSafetyRating function.
One thing that should solve this is to add this to Motor Vehicle:
virtual int getSafetyRating() = 0;
and in the car.h write
virtual int getSafetyRating()
Add declaration of function int getSafetyRating() in your car.h
Basically for some reason new object is wrong type. All source code is on github https://github.com/teuro/sfml-radar. If it's help please fork at will.
I have following class:
#ifndef _VIEW_HPP
#define _VIEW_HPP
#include <iostream>
#include "sfml_drawsurface.hpp"
class View {
protected:
View(Drawsurface& d) : drawer(d) {
std::clog << "View::View()" << std::endl;
}
Drawsurface& drawer;
virtual void draw() = 0;
};
#endif
That is base class for all different kind of views. Now I have derived sub-class
#ifndef _GAME_VIEW_HPP
#define _GAME_VIEW_HPP
#include <vector>
#include <iostream>
#include <typeinfo>
#include "view.hpp"
#include "../models/game.hpp"
class Gameview : public View {
public:
Gameview(Drawsurface& d);
~Gameview();
void draw();
private:
Drawsurface& drawer;
};
#endif // _GAME_VIEW_HPP
Then abstract class Drawsurface
/**
* drawsurface base for all graphics pure abstract
* provide only interface quite high-level
* 2014/06/02
* Juha Teurokoski
**/
#ifndef _DRAWSURFACE_HPP
#define _DRAWSURFACE_HPP
#include <string>
#include "../models/point.hpp"
class Drawsurface {
public:
bool font_loaded;
virtual void rectangleColor(Point& a, Point& b, unsigned int color) = 0;
virtual void lineColor(Point& a, Point& b, unsigned int color) = 0;
virtual void circleColor(Point& a, unsigned int rad, unsigned int color) = 0;
virtual void trigonColor(Point& a, Point& b, Point& c, unsigned int color) = 0;
virtual void trigonColor(Point& a, unsigned int size, unsigned int color) = 0;
virtual void load_font(std::string font) = 0;
virtual void draw_picture(std::string tiedosto, Point& a, bool center = false) = 0;
virtual void draw_text(std::string text, Point& a, unsigned int color = 0) = 0;
virtual int get_fontsize() = 0;
virtual void flip() = 0;
virtual void clear_screen() = 0;
virtual ~Drawsurface() { }
};
#endif
Now if I create new instance of sfml_drawsurface which is sub-class of Drawsurface. For some reason new object is Drawsuface istead of sfml_drawsurface. Below is sfml_drawsurface class.
#ifndef SFML_DRAWSURFACE_HPP
#define SFML_DRAWSURFACE_HPP
/**
* sfml-drawsurface provides basic drawing, pictures and text
* require drawsurface
* 2014/06/02
* Juha Teurokoski
**/
#include "drawsurface.hpp"
#include <vector>
#include <stdexcept>
#include <iostream>
#include <SFML/Graphics.hpp>
class sfml_drawsurface : public Drawsurface {
public:
sfml_drawsurface(sf::RenderWindow& window);
~sfml_drawsurface();
void rectangleColor(Point& a, Point& b, unsigned int color);
void circleColor(Point& a, unsigned int rad, unsigned int color);
void lineColor(Point& a, Point& b, unsigned int color);
void trigonColor(Point& a, Point& b, Point& c, unsigned int color);
void trigonColor(Point& a, unsigned int _size, unsigned int color);
void draw_picture(std::string tiedosto, Point& a, bool center = false);
void draw_text(std::string text, Point& a, unsigned int color);
void load_font(std::string font);
void clear_screen();
int get_fontsize();
void flip();
protected:
private:
sf::RenderWindow& window;
sf::Font font;
sf::Color active;
sf::Color normal;
};
#endif // SFML_DRAWSURFACE_HPP
I create new object like this:
sfml_drawsurface drawer(window);
this->gameview = new Gameview(drawer);
std::clog << typeid(drawer).name() << std::endl;
And everything seems to be right, because std::clog outout is '16sfml_drawsurface'.
Next place is draw-method then happens something really weird.
Same print is now '11Drawsurface'.
Looks like Mike had the right idea. From your Program.cpp file you have in your constructor:
Program::Program() {
Game game;
...
this->gamecontroller = new Gamecontroller(game); //Probably also bad
sfml_drawsurface drawer(window);
this->gameview = new Gameview(drawer);
}
The problem is that drawer ceases to exist once the constructor is finished leaving you with a dangling reference and undefined behaviour. Looks like you may have the same problem with the game variable.
Solution is to not have them as local variables but as either class members (preferred) or dynamically allocated (it depends how long you need to have them around).
I have a little problem, i probably included the class files wrongly, since i can't acces members of the enemy class. What am i doing wrong?
my cpp for class
#include "classes.h"
class Enemy
{
bool alive;
double posX,posY;
int enemyNum;
int animframe;
public:
Enemy(int col,int row)
{
animframe = rand() % 2;
posX = col*50;
posY = row*50;
}
Enemy()
{
}
void destroy()
{
alive = 0;
}
void setposX(double x)
{x = posX;}
void setposY(double y)
{y = posY;}
};
my header for class:
class Enemy;
my main:
#include "classes.h"
Enemy alien;
int main()
{
alien. // this is where intelisense tells me there are no members
}
Your main file will only see what you wrote in the header, which is that Enemy is a class. Normally, you'd declare your whole class with fields and method signatures in the header files, and provide implementations in the .cpp file.
classes.h:
#ifndef _CLASSES_H_
#define _CLASSES_H_
class Enemy
{
bool alive;
double posX,posY;
int enemyNum;
int animframe;
public:
Enemy(int col,int row);
Enemy();
void destroy();
void setposX(double x);
void setposY(double y);
};
#endif
classes.cpp:
#include "classes.h"
//....
void Enemy::destroy(){
//....
}
//....
In addition to Vlad's answer, your file with main doesn't know anything about the Enemy class, other than that it exists.
In general, the class declarations goes in the header file, and the function definitions go in another.
Consider splitting the files like:
classes.h:
#ifndef CLASSES_H
#define CLASSES_H
class Enemy
{
private:
bool alive;
double posX,posY;
int enemyNum;
int animframe;
public:
Enemy(int col,int row);
Enemy();
void destroy();
void setposX(double x);
void setposY(double y);
};
#endif//CLASSES_H
Note the "include guards" which prevent the same file from being included more than once. Good practice to use on header files, or else you get annoying compilation errors.
classes.cpp:
#include "classes.h"
Enemy::Enemy(int col,int row)
{
animframe = rand() % 2;
posX = col*50;
posY = row*50;
}
Enemy::Enemy()
{
}
void Enemy::destroy()
{
alive = 0;
}
void Enemy::setposX(double x) {x = posX;}
void Enemy::setposY(double y) {y = posY;}