• Advertisement

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
Advertisement

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

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)

Edited by MarcusAseth

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


  • Advertisement
  • Advertisement
  • Popular Tags

  • Advertisement
  • Popular Now

  • Similar Content

    • By francoisdiy
      So I wrote a programming language called C-Lesh to program games for my game maker Platformisis. It is a scripting language which tiles into the JavaScript game engine via a memory mapper using memory mapped I/O. Currently, I am porting the language as a standalone interpreter to be able to run on the PC and possibly other devices excluding the phone. The interpreter is being written in C++ so for those of you who are C++ fans you can see the different components implemented. Some background of the language and how to program in C-Lesh can be found here:

      http://www.codeloader.net/readme.html
      As I program this thing I will post code from different components and explain.
    • By isu diss
      I'm trying to duplicate vertices using std::map to be used in a vertex buffer. I don't get the correct index buffer(myInds) or vertex buffer(myVerts). I can get the index array from FBX but it differs from what I get in the following std::map code. Any help is much appreciated.
      struct FBXVTX { XMFLOAT3 Position; XMFLOAT2 TextureCoord; XMFLOAT3 Normal; }; std::map< FBXVTX, int > myVertsMap; std::vector<FBXVTX> myVerts; std::vector<int> myInds; HRESULT FBXLoader::Open(HWND hWnd, char* Filename, bool UsePositionOnly) { HRESULT hr = S_OK; if (FBXM) { FBXIOS = FbxIOSettings::Create(FBXM, IOSROOT); FBXM->SetIOSettings(FBXIOS); FBXI = FbxImporter::Create(FBXM, ""); if (!(FBXI->Initialize(Filename, -1, FBXIOS))) { hr = E_FAIL; MessageBox(hWnd, (wchar_t*)FBXI->GetStatus().GetErrorString(), TEXT("ALM"), MB_OK); } FBXS = FbxScene::Create(FBXM, "REALMS"); if (!FBXS) { hr = E_FAIL; MessageBox(hWnd, TEXT("Failed to create the scene"), TEXT("ALM"), MB_OK); } if (!(FBXI->Import(FBXS))) { hr = E_FAIL; MessageBox(hWnd, TEXT("Failed to import fbx file content into the scene"), TEXT("ALM"), MB_OK); } FbxAxisSystem OurAxisSystem = FbxAxisSystem::DirectX; FbxAxisSystem SceneAxisSystem = FBXS->GetGlobalSettings().GetAxisSystem(); if(SceneAxisSystem != OurAxisSystem) { FbxAxisSystem::DirectX.ConvertScene(FBXS); } FbxSystemUnit SceneSystemUnit = FBXS->GetGlobalSettings().GetSystemUnit(); if( SceneSystemUnit.GetScaleFactor() != 1.0 ) { FbxSystemUnit::cm.ConvertScene( FBXS ); } if (FBXI) FBXI->Destroy(); FbxNode* MainNode = FBXS->GetRootNode(); int NumKids = MainNode->GetChildCount(); FbxNode* ChildNode = NULL; for (int i=0; i<NumKids; i++) { ChildNode = MainNode->GetChild(i); FbxNodeAttribute* NodeAttribute = ChildNode->GetNodeAttribute(); if (NodeAttribute->GetAttributeType() == FbxNodeAttribute::eMesh) { FbxMesh* Mesh = ChildNode->GetMesh(); if (UsePositionOnly) { NumVertices = Mesh->GetControlPointsCount();//number of vertices MyV = new XMFLOAT3[NumVertices]; for (DWORD j = 0; j < NumVertices; j++) { FbxVector4 Vertex = Mesh->GetControlPointAt(j);//Gets the control point at the specified index. MyV[j] = XMFLOAT3((float)Vertex.mData[0], (float)Vertex.mData[1], (float)Vertex.mData[2]); } NumIndices = Mesh->GetPolygonVertexCount();//number of indices MyI = (DWORD*)Mesh->GetPolygonVertices();//index array } else { FbxLayerElementArrayTemplate<FbxVector2>* uvVertices = NULL; Mesh->GetTextureUV(&uvVertices); int idx = 0; for (int i = 0; i < Mesh->GetPolygonCount(); i++)//polygon(=mostly triangle) count { for (int j = 0; j < Mesh->GetPolygonSize(i); j++)//retrieves number of vertices in a polygon { FBXVTX myVert; int p_index = 3*i+j; int t_index = Mesh->GetTextureUVIndex(i, j); FbxVector4 Vertex = Mesh->GetControlPointAt(p_index);//Gets the control point at the specified index. myVert.Position = XMFLOAT3((float)Vertex.mData[0], (float)Vertex.mData[1], (float)Vertex.mData[2]); FbxVector4 Normal; Mesh->GetPolygonVertexNormal(i, j, Normal); myVert.Normal = XMFLOAT3((float)Normal.mData[0], (float)Normal.mData[1], (float)Normal.mData[2]); FbxVector2 uv = uvVertices->GetAt(t_index); myVert.TextureCoord = XMFLOAT2((float)uv.mData[0], (float)uv.mData[1]); if ( myVertsMap.find( myVert ) != myVertsMap.end() ) myInds.push_back( myVertsMap[ myVert ]); else { myVertsMap.insert( std::pair<FBXVTX, int> (myVert, idx ) ); myVerts.push_back(myVert); myInds.push_back(idx); idx++; } } } } } } } else { hr = E_FAIL; MessageBox(hWnd, TEXT("Failed to create the FBX Manager"), TEXT("ALM"), MB_OK); } return hr; } bool operator < ( const FBXVTX &lValue, const FBXVTX &rValue) { if (lValue.Position.x != rValue.Position.x) return(lValue.Position.x < rValue.Position.x); if (lValue.Position.y != rValue.Position.y) return(lValue.Position.y < rValue.Position.y); if (lValue.Position.z != rValue.Position.z) return(lValue.Position.z < rValue.Position.z); if (lValue.TextureCoord.x != rValue.TextureCoord.x) return(lValue.TextureCoord.x < rValue.TextureCoord.x); if (lValue.TextureCoord.y != rValue.TextureCoord.y) return(lValue.TextureCoord.y < rValue.TextureCoord.y); if (lValue.Normal.x != rValue.Normal.x) return(lValue.Normal.x < rValue.Normal.x); if (lValue.Normal.y != rValue.Normal.y) return(lValue.Normal.y < rValue.Normal.y); return(lValue.Normal.z < rValue.Normal.z); }  
    • By Karol Plewa
      Hi, 
       
      I am working on a project where I'm trying to use Forward Plus Rendering on point lights. I have a simple reflective scene with many point lights moving around it. I am using effects file (.fx) to keep my shaders in one place. I am having a problem with Compute Shader code. I cannot get it to work properly and calculate the tiles and lighting properly. 
       
      Is there anyone that is wishing to help me set up my compute shader?
      Thank you in advance for any replies and interest!
    • By fishyperil
      I'm looking for some references that could help me learn how to program some really basic 2D enemy behaviours.
      I wasn't sure whether to post this here or in the AI section but I think it might be more suitable to be posted here since it has more to do with basic maths than any AI related algorithms.
      Could anyone help recommend some resources (books, posts, videos) that could help me understand how to properly implement the basics of enemy movement in 2d games ? So far I've only managed to get them to chase the player character and to stop moving on collision, but the movement is pretty unrealistic and once the collision occurs the enemies all "pile up" on the player character. I'm doing this in C++ so no guides that explain how to script this using an engine api please.
    • By LifeArtist
      Good Evening,
      I want to make a 2D game which involves displaying some debug information. Especially for collision, enemy sights and so on ...
      First of I was thinking about all those shapes which I need will need for debugging purposes: circles, rectangles, lines, polygons.
      I am really stucked right now because of the fundamental question:
      Where do I store my vertices positions for each line (object)? Currently I am not using a model matrix because I am using orthographic projection and set the final position within the VBO. That means that if I add a new line I would have to expand the "points" array and re-upload (recall glBufferData) it every time. The other method would be to use a model matrix and a fixed vbo for a line but it would be also messy to exactly create a line from (0,0) to (100,20) calculating the rotation and scale to make it fit.
      If I proceed with option 1 "updating the array each frame" I was thinking of having 4 draw calls every frame for the lines vao, polygons vao and so on. 
      In addition to that I am planning to use some sort of ECS based architecture. So the other question would be:
      Should I treat those debug objects as entities/components?
      For me it would make sense to treat them as entities but that's creates a new issue with the previous array approach because it would have for example a transform and render component. A special render component for debug objects (no texture etc) ... For me the transform component is also just a matrix but how would I then define a line?
      Treating them as components would'nt be a good idea in my eyes because then I would always need an entity. Well entity is just an id !? So maybe its a component?
      Regards,
      LifeArtist
  • Advertisement