Advertisement

C2280 - Attempting to reference a deleted function

Started by October 07, 2017 05:28 AM
5 comments, last by matt77hias 7 years, 2 months ago

I am confused here... I have 3 classes in place, (code below) - A GameMode, a GameState(base), a MenuState(derived)

Now, the line causing problem is in GameMode.cpp constructor at the line:


GameStates{
		{ EGameState::MAIN_MENU, std::make_unique<MenuState>(this, windowInfo)}
}

and the error mentioning "deleted function" clearly has to do with some of my = delete for copy and move constructors, but I fail to see the connection.

For what I can tell, I am calling the initializer list constructor of a map<>,  and the make_unique should be returning an rvalue which should be moving the unique_ptr into the initializer list, so I just can't see how my deleted function are involved in this.

But since the error is there, something else is going on, and if is not my deleted function, then it is something with the unique_ptr. Can you see what it is? :)

 

GameMode.h:


#pragma once
#include <map>
#include <memory>
class GameState;

class GameMode
{
	enum class EGameState{MAIN_MENU,PLAY};
private://variables
	Util::WindowInfo WindowInfo;
	GameState* CurrState;
	std::map<EGameState, std::unique_ptr<GameState>> GameStates;

public://methods
	GameMode(Util::WindowInfo windowInfo);
	~GameMode();
	GameMode(const GameMode&) = delete;
	GameMode& operator=(const GameMode&) = delete;
	GameMode(GameMode&&) = delete;
	GameMode& operator=(GameMode&&) = delete;

	GameState* CurrentState()const { return CurrState; }
	void SetCurrentState(EGameState newState);
};

GameMode.cpp


#include "GameState.h"
#include "MenuState.h"
#include "PlayState.h"
#include "Utility.h"
#include "GameMode.h"


GameMode::GameMode(Util::WindowInfo windowInfo) :
	CurrState{ nullptr },
	GameStates{
		{ EGameState::MAIN_MENU, std::make_unique<MenuState>(this, windowInfo)}
}
{
	SetCurrentState(EGameState::MAIN_MENU);
}


GameMode::~GameMode()
{
}

void GameMode::SetCurrentState(EGameState newState)
{
	if (GameStates.find(newState) != GameStates.end()) {
		CurrState = GameStates[newState].get();
	}
}

 

GameState.h  (base class for states):


#pragma once
#include "Utility.h"

class GameMode;

class GameState
{
protected:
	Util::WindowInfo WindowInfo;
	GameMode* Game;
public:
	GameState(GameMode* game,Util::WindowInfo windowInfo);
	virtual ~GameState();
  
	virtual void Update(float deltaTime) = 0;
	virtual void Draw(float interpolation) = 0;
};

GameState.cpp (base class for states):


#include "GameState.h"

GameState::GameState(GameMode* game, Util::WindowInfo windowInfo):
	Game{game}, WindowInfo { windowInfo }
{
}

GameState::~GameState()
{
}

MenuState (derived from GameState):


#pragma once
#include "GameState.h"
class MenuState : public GameState
{
public:
	MenuState(GameMode* game, Util::WindowInfo windowInfo);
	~MenuState();
	MenuState(const MenuState&) = delete;
	MenuState& operator=(const MenuState&) = delete;
	MenuState(MenuState&&) = delete;
	MenuState& operator=(MenuState&&) = delete;

	void Update(float deltaTime) override;
	void Draw(float interpolation) override;
};

MenuState.cpp (derived from GameState)


#include "MenuState.h"
#include "SDL2\SDL.h"


MenuState::MenuState(GameMode* game, Util::WindowInfo windowInfo):
	GameState(game, windowInfo)
{
}


MenuState::~MenuState()
{
}

void MenuState::Update(float deltaTime)
{
}

void MenuState::Draw(float interpolation)
{
	SDL_SetRenderDrawColor(WindowInfo.Renderer, 30, 30, 50, 255);
	SDL_RenderClear(WindowInfo.Renderer);
	SDL_RenderPresent(WindowInfo.Renderer);
}

 

Ok, what I said above seems pretty close to the truth of things, because switching unique_ptr into shared_ptr solved the bug.

Therefore, somewhere one that line there is an attempt to copy a unique_ptr instead of moving it, and I don't understand where.

Any idea? :)

Advertisement

Here I've made the simplest possible representation of my code/problem you can test with, try to compile to get the same error:

So what is going wrong here? xD

How would you make the code below compile, while retaining the unique_ptr?


#include <string>
#include <map>
#include <memory>
using namespace std;

int main() {
	map<string, unique_ptr<int>> MyMap{
		{ "first" ,make_unique<int>(6)  },
		{ "second",make_unique<int>(22) },
		{ "third" ,make_unique<int>(86) }
	};
}

 

Look at the constructor of your std::map. You use an initializer list. Look at the value type of the key-value pairs stored in your std::map, it is a std::unique_ptr. std::unique_ptrs are non-copyable, only movable.

You cannot move objects out of initializer lists, since they only allow const access to their members. As such, you cannot use initializer lists and move constructors; they can only be copied.

 

🧙

this is what I was suspecting...so no clever work arounds, I assume? Well...I'll use shared_ptr then (or an uglier initialization style x_x)

17 minutes ago, MarcusAseth said:

this is what I was suspecting...so no clever work arounds, I assume? Well...I'll use shared_ptr then (or an uglier initialization style x_x)

You can move one element at the time to your std::map or construct the elements in place in your std::map.

🧙

This topic is closed to new replies.

Advertisement