MarcusAseth

C2280 - Attempting to reference a deleted function

Recommended Posts

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);
}

 

Edited by MarcusAseth

Share this post


Link to post
Share on other sites

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? :)

Share this post


Link to post
Share on other sites

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) }
	};
}

 

Edited by MarcusAseth

Share this post


Link to post
Share on other sites

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.

 

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now


  • Announcements

  • Forum Statistics

    • Total Topics
      628379
    • Total Posts
      2982353
  • Similar Content

    • By Kazuma506
      I am trying to recreate the combat system in the game Life is Feudal but make it more complex. The fighting system works by taking in the direction of the mouse movement and if you press the left click it will swing in that direction, though stab, overhead, left (up, down, left right) and right are the only swings that you can do. If you wanted to you could also hold the swing by holding left click so you are able to swing at the perfect moment in the battle. I want to change this so add in more swing directions but I also want to code this from scratch in Unreal. Can anyone give me any pointers or maybe a few snippets of code that work in Unreal that could help me start to implement this type of system?
       
       
    • By rXpSwiss
      Hello,
      I am sending compressed json data from the UE4 client to a C++ server made with boost.
      I am using ZLib to compress and decompress all json but it doesn't work. I am now encoding it in base64 to avoid some issues but that doesn't change a thing.
      I currently stopped trying to send the data and I am writing it in a file from the client and trying to read the file and decompress on the server side.
      When the server is trying to decompress it I get an error from ZLib : zlib error: iostream error
      My question is the following : Did anyone manage to compress and decompress data between a UE4 client and a C++ server ?
      I cannot really configure anything on the server side (because boost has its ZLib compressor) and I don't know what is wrong with the decompression.
      Any idea ?
      rXp
    • By noodleBowl
      I was wondering if someone could explain this to me
      I'm working on using the windows WIC apis to load in textures for DirectX 11. I see that sometimes the WIC Pixel Formats do not directly match a DXGI Format that is used in DirectX. I see that in cases like this the original WIC Pixel Format is converted into a WIC Pixel Format that does directly match a DXGI Format. And doing this conversion is easy, but I do not understand the reason behind 2 of the WIC Pixel Formats that are converted based on Microsoft's guide
      I was wondering if someone could tell me why Microsoft's guide on this topic says that GUID_WICPixelFormat40bppCMYKAlpha should be converted into GUID_WICPixelFormat64bppRGBA and why GUID_WICPixelFormat80bppCMYKAlpha should be converted into GUID_WICPixelFormat64bppRGBA
      In one case I would think that: 
      GUID_WICPixelFormat40bppCMYKAlpha would convert to GUID_WICPixelFormat32bppRGBA and that GUID_WICPixelFormat80bppCMYKAlpha would convert to GUID_WICPixelFormat64bppRGBA, because the black channel (k) values would get readded / "swallowed" into into the CMY channels
      In the second case I would think that:
      GUID_WICPixelFormat40bppCMYKAlpha would convert to GUID_WICPixelFormat64bppRGBA and that GUID_WICPixelFormat80bppCMYKAlpha would convert to GUID_WICPixelFormat128bppRGBA, because the black channel (k) bits would get redistributed amongst the remaining 4 channels (CYMA) and those "new bits" added to those channels would fit in the GUID_WICPixelFormat64bppRGBA and GUID_WICPixelFormat128bppRGBA formats. But also seeing as there is no GUID_WICPixelFormat128bppRGBA format this case is kind of null and void
      I basically do not understand why Microsoft says GUID_WICPixelFormat40bppCMYKAlpha and GUID_WICPixelFormat80bppCMYKAlpha should convert to GUID_WICPixelFormat64bppRGBA in the end
       
    • By HD86
      As far as I know, the size of XMMATRIX must be 64 bytes, which is way too big to be returned by a function. However, DirectXMath functions do return this struct. I suppose this has something to do with the SIMD optimization. Should I return this huge struct from my own functions or should I pass it by a reference or pointer?
      This question will look silly to you if you know how SIMD works, but I don't.
    • By pristondev
      Hey, Im using directx allocate hierarchy from dx9 to use a skinned mesh system.
      one mesh will be only the skeleton with all animations others meshes will be armor, head etc, already skinned with skeleton above. No animation, idle position with skin, thats all I want to use the animation from skeleton to other meshes, so this way I can customize character with different head, armor etc. What I was thinking its copy bone matrices from skeleton mesh to others meshes, but Im a bit confused yet what way I can do this.
       
      Thanks.
  • Popular Now