SFML getting window to another class via constructor not working - c++

I'm trying to create an object where the SFML RenderWindow object is passed over as a parameter, but it isn't simply working, it complains all the time about pointer and that I'm using them in the wrong way.
Here you have my .h file:
#include <iostream>
#include <SFML\System.hpp>
#include <SFML\Window.hpp>
#include <SFML\Graphics.hpp>
using namespace sf;
class Shot
{
private:
RenderWindow &mainWindow;
public:
Shot(RenderWindow &window);
void add(float x, float y, float velocity);
};
and here my .cpp
#include "Shot.h"
Shot::Shot(RenderWindow &window) : mainWindow(&window)
{
mainWindow -> window;
}
void Shot::add(float x, float y, float velocity)
{
CircleShape shape(10);
shape.setPosition(Vector2f(x, y));
shape.setFillColor(Color::Yellow);
mainWindow.draw(shape);
}
Errors:
Error 1 error C2248: 'sf::NonCopyable::operator =' : cannot access private member declared in class 'sf::NonCopyable'
Error 2 error C2248: 'sf::NonCopyable::operator =' : cannot access private member declared in class 'sf::NonCopyable'
i honestly have no clue what's the problem by now an I've probably done it all wrong, but any help would really be appreciated! :)
Best Regards
FreeSirenety

In your .cpp file, you do:
Shot::Shot(RenderWindow &window) : mainWindow(&window)
{
mainWindow -> window;
}
but window is a reference, so you simply can do:
Shot::Shot(RenderWindow &window) : mainWindow(window)
{}
Also, I wouldn't use using namespace sf;, it can make the code confusing after awhile.

Related

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.

C++ public inheritance [SFML]

I've been having trouble drawing a sprite of hero to the screen(which I solved with a different approach). Problem was caused because of inheritance. Now I'm gonna put all the code and tell the story alongside with codes.
Character.h (the parent class for hero and npcs)
#pragma once
#include <SFML/Graphics.hpp>
#include "TextureHolder.h"
using namespace sf;
using namespace std;
class Character
{
protected:
Vector2f m_Position;
int m_Speed;
public:
Character();
void spawn(int x, int y, string direction);
Sprite m_Sprite;
Texture m_Texture;
void virtual update(float elapsedTime);
};
Character.cpp has nothing except constructor and update function declerations
#include "stdafx.h"
#include "Character.h"
Character::Character(){}
void Character::update(float elapsedTime) {}
Hero.h
#pragma once
#include "Character.h"
using namespace sf;
using namespace std;
class Hero : public Character
{
private:
public:
Hero();
void virtual update(float elapsedTime);
};
And finally Hero.cpp
#include "stdafx.h"
#include "Hero.h"
Hero::Hero()
{
m_Texture = TextureHolder::GetTexture("images/chars/hero.png");
m_Sprite.setTexture(m_Texture);
}
void Hero::update(float elapsedTime)
{
}
With the classes this way, When I try to draw hero.m_Sprite it gives access violation.
As you see I'm using public inheritance from character to hero(which should have worked perfectly, since when I type "hero." it shows me list of it's public members and m_sprite is among them)
But when I try to add m_Sprite and m_Texture in Hero.h(instead of inheriting them from Character class , I got no errors.
What is the reason behind this?
Edit: draw.cpp
#include "stdafx.h"
#include "Engine.h"
void Engine::draw()
{
m_Window.clear(Color::Black);
m_Window.setView(m_MainView);
m_Window.draw(hero.m_Sprite);
m_Window.display();
}
EDIT 2: IT doesn't give errors anymore. I didn't change anything and I don't have any idea what caused and what fixed the problem.

Unresolved external symbol _main referenced

i'm working on an intake assignment for a game development course but i'm stuck with an annoying error by visual studio after splitting my code into classes. The error states LNK2019 unresolved external symbol _main referenced in function "int __cdecl invoke_main(void)" (?invoke_main##YAHXZ) So it should be something wrong in my main function. I've looked around and the most logical reason seemt to be wrong subsystem but that's set to console application. Here's my code:
Main.cpp:
#include <Box2D/Box2D.h>
#include <SFML/Graphics.hpp>
#include "Game.hpp"
int main() {
Game game; //Sets up the game
game.populateWorld();
while (game.window.isOpen()) {
game.checkEvents();
game.drawBodies();
game.m_Player->movePlayer();
}
return 0;
}
Game.hpp:
#ifndef Game_hpp
#define Game_hpp
#include <SFML/Graphics.hpp>
#include "World.hpp"
#include "Player.hpp"
class Game {
public:
Game();
void checkEvents();
void drawBodies();//Displays the bodies and keeps the view centered
//Public data members to be used by other classes
sf::Texture backGroundTexture;
sf::Texture playerTex;
sf::Texture platformTex;//Doesn't have a sprite declared since multiple copy's will be made
sf::Sprite backGround;
sf::Sprite player;
sf::View view;
sf::RenderWindow window;
const float m_Scale;//Number of pixels per meter
Player* m_Player = NULL;
World* m_World = NULL;
void populateWorld();
private:
void loadResources();//Loads resources and applies them where needed
};
#endif // !Game_hpp
World.hpp:
#ifndef World_hpp
#define World_hpp
#include <Box2D/Box2D.h>
class World {
public:
//Public member functions
World(const float scale);
~World();
void step();
b2Body* spawnPlayer(float x, float y);
//Public data members
b2World* world = NULL; //Points to the entire world! Handle with care ;)
void createPlatform(b2Vec2 point);
private:
//Private data members
b2Vec2 gravity;
const float m_Scale;
//Declaring step values
float32 m_TimeStep;
int32 m_VelocityIterators;
int32 m_PositionIterators;
};
#endif // !World_hpp
Player.hpp:
#ifndef Player_hpp
#define Player_hpp
#include <Box2D/Box2D.h>
#include "World.hpp"
class Player{
public:
Player(World* world, const float scale, float x, float y);
void movePlayer();
b2Body* m_PlayerBody;//A pointer to the player Character's body needed for the movement
private:
World* m_World;
const float m_Scale;
};
#endif // !Player_hpp
Hope anyone can point me in the right direction with what's going wrong :)
Guess i was ignorant... There was a warning about the main.cpp using line endings for Mac Os X since i work on an iMac i didn't care. But when i fixed this in Notepad++ EDIT -> EOL Conversion -> Windows Format, it worked like a sharm! My bad i'm afraid ^^
I solved my problem by the following:
1) closing the project and restarting the PC
2) Creating a new project and following this thread because I was doing GUI
http://www.bogotobogo.com/cplusplus/application_visual_studio_2013.php

"does not have a class type" error

Player class:-
#ifndef PLAYER_H
#define PLAYER_H
class Player : public sf::Drawable, sf::Transformable
{
public:
Player(int indexWd, int indexHi);
bool load();
};
Game class :-
#ifndef GAME_H
#define GAME_H
#include "Player.h"
#include <SFML/Graphics.hpp>
class Game
{
public:
Game();
Player player(int indWd, int indHi);
void load();
};
#endif // GAME_H
Inside Game cpp file :-
#include "Game.h"
Game::Game()
{
}
void Game::load()
{
player(world.getIndexWd(), world.getIndexHi());
player.load(); //gives error
}
at player.load() in the above method, compiler is giving the following error :-
error: '((Game*)this)->Game::player' does not have class type|
Why is this error happening ?
You've declared a member function called player, and you're trying to use that as if it were a variable.
I'm guessing that it's supposed to be a data member:
Player player;
initialised in the load function:
player = Player(world.getIndexWd(), world.getIndexHi());
player.load();
Although this won't work since it doesn't have a default constructor, so must be initialised in the constructor:
Game::Game() : player(world.getIndexWd(), world.getIndexHi()) {}
which will only work if world has been initialised at this point.
Rewrite as:
void Game::load()
{
Player p = this->player(world.getIndexWd(), world.getIndexHi());
p.load();
}
player is a member function in current class and you can not apply .load on it.
You did not define your function
Player player(int indWd, int indHi);
in your .cpp file.
Also you need to either make it static in your Player class, or use an object to call it.
Player p = player(world.getIndexWd(), world.getIndexHi());
p.load();
Possible would also be
player(world.getIndexWd(), world.getIndexHi()).load();
but I would not recommend using it, as your Player object will be destroyed afterwards.

C++: circular dependency issue

I'm having a problem with circular dependencies, I suppose this is a design flaw from introducing the Game class in the wrong way.
Game.h:
#pragma once
#include <SFML\Graphics.hpp>
#include "GameScreen.h"
#include "TitleScreen.h"
class Game
{
protected:
sf::RenderWindow window;
GameScreen* CurrentGameScreen;
TitleScreen Title;
public:
Game(void);
~Game(void);
sf::RenderWindow getWindow(void);
void Run();
void Close();
};
GameScreen.h:
#pragma once
#include "Game.h"
class GameScreen
{
public:
GameScreen(void);
~GameScreen(void);
virtual void LoadAllResources() {};
virtual void DrawScreen(Game* game) {};
virtual void Update(Game* game) {};
};
TitleScreen.h:
#pragma once
#include <SFML\Graphics.hpp>
#include "GameScreen.h"
class TitleScreen : public virtual GameScreen
{
private:
sf::Texture title_screen;
sf::Sprite titleScreen;
sf::Font font;
sf::Text menuExit;
public:
TitleScreen(void);
~TitleScreen(void);
void LoadAllResources();
void DrawScreen(Game* game);
void Update(Game* game);
};
Then there's the main file:
#include "Game.h"
int main()
{
Game game;
game.Run();
//sf::RenderWindow window(sf::VideoMode(800, 600), "Test");
//GameScreen* currentScreen;
//TitleScreen titleScreen;
//currentScreen = &titleScreen;
//while (window.isOpen())
//{
// currentScreen->Update(&window);
// currentScreen->DrawScreen(&window);
//}
return 0;
}
GameScreen.h and TitleScreen.h raise a handful of C2061. From what I understand these are caused by circular dependencies between Game.h and Gamescreen.h.
TitleScreen.h is giving me error C2504: 'GameScreen' : base class undefined.
Game.h: on lines 12 and 13, give C2143: syntax error : missing ';' before '*', although I'm not sure where this is coming from and my IDE is not giving me any syntax errors.
If I remove the #include statement from GameScreen.h and substitute it with a forward declaration class Game; (which I guess breaks the circular dependency?) most of the above is solved, but TitleScreen.cpp throws a set of C2027, C2227 and C2228 (undefined type, left of -> and left of .) every time I try to access a Game object. IntelliSense points out that a pointer to an incomplete class is not allowed.
I got it working before introducing the Game class - DrawScreen() and Update() would take as argument a pointer to window (sf::RenderWindow* window). There's part of the old code left in main.cpp.
In the GameScreen.h you should declare the Game class instead of including its whole header file, so this:
class Game;
instead of:
#include "Game.h"