initialization list c++ struct - c++

I have a problem with some warnings when I compile my code. This is the file where the warnings are coming from:
#include <memory>
#include <string>
#include <SFML/Graphics.hpp>
#include "StateMachine.hpp"
#include "AssetManager.hpp"
#include "InputManager.hpp"
#ifndef GAME_HPP
#define GAME_HPP
struct GameData {
StateMachine machine;
sf::RenderWindow window;
AssetManager assets;
InputManager input;
};
typedef std::shared_ptr<GameData> GameDataRef;
class Game {
public:
Game(int width, int height, std::string title);
private:
const float dt = 1.0f / 3600.0f;
sf::Clock _clock;
GameDataRef _data = std::make_shared<GameData>();
void Run();
};
#endif
From the terminal the warnings are stated as this:
Game.hpp:11:8: warning: ?GameData::input? should be initialized in the member initialization list [-Weffc++]!
The same warning goes for window, machine and assets aswell.
I don't understand how I should initialize them as they already are initialized in the struct? Would someone be kind and explain a way to handle this?

Related

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

"Attribute is protected within this context" with inheritance and .h and .cpp files

I'm trying to code a game with Irrlicht, but I got stuck with this problem meanwhile I was separating the code into .h and .cpp files.
The main error in the compiler is 'node is protected within this context".
Node is an attribute of "GameObjectOverworld", and was invoked from "Player" (GameObjectOverworld child class)
This worked fine until I separated the code in .h and .cpp files.
The GameObjectOverworld.h
#ifndef __GAMEOBJECTOVERWORLD_H__
#define __GAMEOBJECTOVERWORLD_H__
#include <irrlicht.h>
#include <stdio.h>
#include "GameObject.h"
class GameObjectOverWorld : public GameObject{
protected:
scene::ISceneNode* node = nullptr;
public:
GameObjectOverWorld() {}
core::vector3df getPosition(){return node->getPosition();}
};
#endif
The player.h
#ifndef __PLAYER_H__
#define __PLAYER_H__
#include <irrlicht.h>
#include <stdio.h>
#include "GameObject.h"
#include "GameObjectOverworld.h"
using namespace irr;
using namespace core;
using namespace scene;
using namespace video;
using namespace io;
using namespace gui;
class Player : public GameObjectOverWorld{
private:
std::string name ="";
float speed = 15.0f;
public:
Player() = default;
void addPlayerModel(ISceneManager* smgraux, IVideoDriver* driveraux){}
void move (char axis, int direction, float frameDeltaTime){}
};
#endif
And player.cpp (the one which sends the error)
#include "Player.h"
void addPlayerModel(ISceneManager* smgraux, IVideoDriver* driveraux){
Player::node = smgraux->addCubeSceneNode(10.0f, 0, 0, core::vector3df(15.0f, 0.0f, 45.0f), core::vector3df(0, 0, 0), core::vector3df(1.0f, 1.0f, 1.0f));
if (node)
{
node->setMaterialTexture(0, driveraux->getTexture("Materials/madero.jpg"));
node->setMaterialFlag(video::EMF_LIGHTING, false);
}
}
You forgot to link the method addPlayerModel to the class:
Change:
void addPlayerModel(ISceneManager* smgraux, IVideoDriver* driveraux)
To
void Player::addPlayerModel(ISceneManager* smgraux, IVideoDriver* driveraux)
Also change Player::node to this->node because your 'node' attribute is not static.

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

class not declared in scope but the class is declared

I don't know how to describe this but I declare a class (movableBox) but am not able to use in a specific scope (this scope is in the class player).
Here is some of my code:
player class:
#ifndef PLAYER_H
#define PLAYER_H
#include <SFML/Graphics.hpp>
#include <vector>
#include "movablebox.h"
class player
{
public:
sf::RectangleShape* player;
sf::Texture* playerTexture;
sf::Vector2f speed;
bool touching;
static void create(sf::Vector2f pos_);
static void draw(sf::RenderWindow& window);
static void updatePos(float& dt, float gravity, std::vector<sf::RectangleShape*> solids, std::vector<movableBox::movableBox_*> movableBoxes);
static void checkControls();
static void checkControlsperEvent (sf::Event& event);
};
#endif // PLAYER_H
and the movableBox class:
#ifndef MOVABLEBOX_H
#define MOVABLEBOX_H
#include <SFML/Graphics.hpp>
#include <vector>
#include "player.h"
class player;
class movableBox
{
public:
struct movableBox_ {
sf::RectangleShape* box;
sf::Texture* texture;
sf::Vector2f speed;
bool selected;
player* selectedBy;
bool touching;
};
static void create(sf::Vector2f pos_, sf::Vector2f size_);
static void draw(sf::RenderWindow& window);
static void updatePos(float& dt, float gravity, std::vector<sf::RectangleShape*> solids);
static void grabNearest(player* player);
};
#endif // MOVABLEBOX_H
I'm getting this error:
CodeBlocksProjects/phys/player.h:18:110: error: ‘movableBox’ was not declared in this scope
As you can tell by my lack of explaination I don't know why or how this happens, I hope you understand my problem.
Thanks in advance! :)
This is a circular dependency issue. There's #include "movablebox.h" in player.h, and also #include "player.h" in movablebox.h.
You have forward delcared class player in movablebox.h and it seems to be enough; so just remove #include "player.h" from movablebox.h.

sharing an object with multiple classes

I have 3 classes namely Board, Game, and AI
i want to have the object chessBoard of Board class to be use by Game and AI, and also by the Board Class. I want them to access that single chessBoard object (sharing)
problem is, it gives me the infamous fatal error LNK1169: one or more multiply defined symbols found. Also, there are Board.h,Game.h and AI.h (only declarations in them), and i also have their corresponding .cpp files. Each .h files have guards included (#ifndef _XXXXXX_H_)
I tried to include Board chessBoard inside Board.h file (just below the class), and it seems guards are not working.
Error 7 error LNK2005: "class Board chessBoard" (?chessBoard##3VBoard##A) already defined in AI.obj C:\Users\xxxx\Documents\Visual Studio 2010\Projects\CHESSv3\CHESSv3\Board.obj CHESSv3
Error 8 error LNK2005: "class Board chessBoard" (?chessBoard##3VBoard##A) already defined in AI.obj C:\Users\xxxxx\Documents\Visual Studio 2010\Projects\CHESSv3\CHESSv3\Game.obj CHESSv3
Error 9 error LNK2005: "class Board chessBoard" (?chessBoard##3VBoard##A) already defined in AI.obj C:\Users\xxxxx\Documents\Visual Studio 2010\Projects\CHESSv3\CHESSv3\ProgramEntryPoint.obj CHESSv3
AI.h
#ifndef _AI_H_
#define _AI_H_
#include <iostream>
#include <string>
using namespace std;
struct node {
string position;
string nextPosition;
float score;
int level;
float totalscore;
node* up;
node* right;
node* left;
bool approved;
string move;
};
class AI {
private:
//string board;
//string board[8][8];
int score1;
int maxscore;
int totalscore;
public:
void GetBoard(string[][8]);
void AnalyzeMyPositions();
void ExecuteAdvanceHeuristicMove();
};
#endif
Game.h
#ifndef _GAME_H_
#define _GAME_H_
#include <iostream>
#include <string>
#include <vector>
#include <sstream>
#include <queue>
#include <stack>
#include <cmath>
using namespace std;
class Game {
public:
char WhosTurn();
bool Playable();
bool GetMoveFromPlayer();
void TurnOver();
Game();
private:
char turn;
};
#endif
Board.h
#ifndef _BOARD_H_
#define _BOARD_H_
#include <iostream>
#include <string>
#include <vector>
#include <sstream>
#include <queue>
#include <stack>
#include <cmath>
using namespace std;
class Board {
public:
bool SquareChecker(string);
bool MoveChecker(string);
Board();
void PrintBoard();
int Execute(string);
void UnExecute();
string CB[8][8];
private:
char turn;
vector<string> BoardRecord;
stack<string> CBR;
//string CB[8][8];
};
Board chessBoard;
#endif
If you really want to do this, you need to make the declaration of the object extern in the header Board.h:
extern Board chessBoard;
You then provide a declaration in Board.cpp:
Board chessBoard;
Much better, though would be to create it in enclosing code and pass it (by reference) to the constructors of the other classes.
What you are looking for is the Singleton design pattern which can achieved in the following way:
// Board.h
class Board {
private:
static instance_;
public:
static Board *instance();
}
// Board.cpp
Board *Board::instance_ = NULL;
Board *Board::instance() {
if (!instance_)
instance_ = new Board();
return instance_;
}
Mind that this pattern can either be seen as good or bad, if you don't like using it you should consider passing the reference of an instantiated Board class to all the requiring ones and keep it stored in each object as an instance variable. Something like:
Game::Game() {
this->board = new Board();
this->ai = new AI(board);
// or better this->ai = new AI(this) so AI can access all game methods
}
Your problem could be that there could be 3 different chessBoard definitions because you might be adding 3 different #include "Board.h". Please make only a single object at a place where you have more control rather than creating it globally inside Board.h
Did you try it like this? Include the necessary include declarations only in the .cpp files.
//Board.h
class Board {};
//Game.h
class Board;
class Game {
Board* myBoard;
public:
void setBoard(Board*);
};
//AI.h
class Board;
class AI {
Board* myBoard;
public:
void setBoard(Board*);
};
void main() {
Board chessBoard;
Game g;
g.setBoard(&chessBoard);
AI ai;
ai.setBoard(&chessBoard);
}