0

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?

IGP
  • 14,160
  • 4
  • 26
  • 43
Anna
  • 23
  • 2
  • You can add a constructor to GameData in the following way: `GameData() : input() {}`. This uses the member initialization list to construct the InputManager. – Aedoro Dec 13 '21 at 17:30
  • Oh that worked! Thanks!! – Anna Dec 13 '21 at 17:36
  • 1
    Your code was already correct btw: https://stackoverflow.com/questions/14002454/can-i-ignore-the-gcc-warning-foom-bar-should-be-initialized-in-the-member-i – Aedoro Dec 13 '21 at 17:39

0 Answers0