Error: Attempting to reference a deleted function

The Partridge Family were neither partridges nor a family. Discuss.
Post Reply
turbosheep4
Posts: 2
Joined: February 17th, 2017, 4:51 pm

Error: Attempting to reference a deleted function

Post by turbosheep4 » February 17th, 2017, 5:02 pm

Hey i am fairly new to c++ and very new to directX but i have encountered an error that doesnt make much sense to me.

For some reason in the lines in updateModel in the game class where i am passing wnd into the method in box I am getting this very strange error "attempting to reference a deleted function".

I find this strange because the wnd object is working fine inside the updateModel variable. Could you tell me how to fix this and what exactly the problem is so that i can avoid it in the future.

Here is all the code (I am using the chili directX framework):

Game Class:

Code: Select all

#include "MainWindow.h"
#include "Game.h"

Game::Game( MainWindow& wnd )
	:
	wnd( wnd ),
	gfx( wnd )
{
}

void Game::Go()
{
	gfx.BeginFrame();	
	UpdateModel();
	ComposeFrame();
	gfx.EndFrame();
}

void Game::UpdateModel()
{
	//game logic here

	//MOVE BOXES
	
	player1.Move(wnd, gfx.ScreenWidth, gfx.ScreenHeight); // these two lines are giving the 
                                                                                     //error because of the wnd variable
	player2.Move(wnd, gfx.ScreenWidth, gfx.ScreenHeight);
}

void Game::ComposeFrame()
{
	//frame drawing here
}
Game header

Code: Select all

#pragma once

#include "Keyboard.h"
#include "Mouse.h"
#include "Graphics.h"
#include "box.h"

class Game
{
public:
	Game( class MainWindow& wnd );
	Game( const Game& ) = delete;
	Game& operator=( const Game& ) = delete;
	void Go();
private:
	void ComposeFrame();
	void UpdateModel();
	/********************************/
	/*  User Functions              */
	/********************************/
private:
	MainWindow& wnd;
	Graphics gfx;
	/********************************/
	/*  User Variables              */

	//create the two boxes
	box player1 = box(100, 100, 255, 125, 125),
		player2 = box(500, 100, 0, 200, 200);

	/********************************/
};
Box class:

Code: Select all

#include "box.h"
#include "MainWindow.h"



box::box(int xPos, int yPos, int red, int green, int blue)
{
	//constructor
	x = xPos;
	y = yPos;
	r = red;
	g = green;
	b = blue;
}

//destructor
box::~box() {}


void box::Draw()
{

}

/****************************************/
/**************PUBLIC METHODS************/
/****************************************/

void box::Move(MainWindow wnd, int screenWidth, int screenHeight)
{
	box::CalcVelocity(wnd);
	box::CalcFriction();
	box::ClampToScreen(screenWidth, screenHeight);
}
/*****************************************/
/**************PRIVATE METHODS************/
/*****************************************/

void box::CalcVelocity(MainWindow wnd)
{
	//if an arrow key is being preseed, accelerate the box
	if (wnd.kbd.KeyIsPressed(VK_UP))
		yVelocity -= ACCELERATION;
	else if (wnd.kbd.KeyIsPressed(VK_DOWN))
		yVelocity += ACCELERATION;

	if (wnd.kbd.KeyIsPressed(VK_LEFT))
		xVelocity -= ACCELERATION;
	else if (wnd.kbd.KeyIsPressed(VK_RIGHT))
		xVelocity += ACCELERATION;

	//do not accelerate past a certain speed
	if (xVelocity > MAXSPEED)
		xVelocity = MAXSPEED;
	if (yVelocity > MAXSPEED)
		yVelocity = MAXSPEED;

	//space key to come to a complete, instantaneous stop
	if (wnd.kbd.KeyIsPressed(VK_SPACE))
	{
		xVelocity = 0;
		yVelocity = 0;
	}
}

void box::CalcFriction(){
	//'friction'
	//friction for x velocity
	if (std::abs(xVelocity) > FRICTION)

		if (xVelocity > 0)
			xVelocity -= FRICTION;
		else
			xVelocity += FRICTION;

	else if (std::abs(xVelocity) != 0 && std::abs(xVelocity) < FRICTION)
		xVelocity = 0;

	//friction for y velocity 
	if (std::abs(yVelocity) > FRICTION)

		if (yVelocity > 0)
			yVelocity -= FRICTION;
		else
			yVelocity += FRICTION;

	else if (std::abs(yVelocity) != 0 && std::abs(yVelocity) < FRICTION)
		yVelocity = 0;

}

void box::ClampToScreen(int screenWidth, int screenHeight)
{
	//change position by velocity, do not let the recticle go off the screen 
	//x velocity
	//right side of screen
	if (x + 5 + xVelocity >= screenWidth)
	{
		xVelocity = (-1)*xVelocity;
		xVelocity *= REBOUND_EFFICIENCY;
		x = screenWidth - 5;
	}
	//left side of screen
	else if (x - 5 + xVelocity <= 0)
	{
		xVelocity = (-1)*xVelocity;
		xVelocity *= REBOUND_EFFICIENCY;
		x = 5;
	}
	else
		x += xVelocity;

	//y velocity
	//bottom of screen
	if (y + 7 + yVelocity >= screenHeight)
	{

		yVelocity = (-1)*yVelocity;
		yVelocity *= REBOUND_EFFICIENCY;
		y = screenHeight - 7;

	}
	//top of screen
	else if (y - 7 + yVelocity <= 0)
	{
		yVelocity = (-1)*yVelocity;
		yVelocity *= REBOUND_EFFICIENCY;
		y = 7;
	}
	else
		y += yVelocity;
}

/****************************************/
/**************STATIC METHODS************/
/****************************************/

static bool box::detectCollision(box, box)
{

	return true;
}
Box Header:

Code: Select all

#pragma once
#include "MainWindow.h"

class box
{
public:
	//constructor and destructor
	box(int xPos, int yPos, int red, int green, int blue);
	~box();

public:
	/****************************************/
	/*************public variables***********/
	/****************************************/


	/****************************************/
	/*************public methods*************/
	void Draw();
	void Move(MainWindow, int, int);
	/****************************************/

private:
	/****************************************/
	/*************private variables**********/
	int r, g, b;
	int x, y;
	float xVelocity, yVelocity;
	const float ACCELERATION = 0.2, 
		FRICTION = 0.08, 
		MAXSPEED = 7, 
		REBOUND_EFFICIENCY = 0.7;

	/****************************************/


	/****************************************/
	/*************private methods************/
	void CalcFriction();
	void ClampToScreen(int width, int height);
	void CalcVelocity(MainWindow);
	/****************************************/

///////////////////////////////////////////////////////

	/****************************************/
	/*************static members*************/
	static bool detectCollision(box, box);
	/****************************************/


};

Note: Yes I know both boxes will be controlled by the arrow keys, im going to fix that.

Thanks in advance :))

User avatar
Yumtard
Posts: 575
Joined: January 19th, 2017, 10:28 pm
Location: Idiot from northern Europe

Re: Error: Attempting to reference a deleted function

Post by Yumtard » February 17th, 2017, 5:14 pm

noob here,
try to initialize the boxes in the game construcor.

If this is not the problem then wait for someone else to respond :D

User avatar
Yumtard
Posts: 575
Joined: January 19th, 2017, 10:28 pm
Location: Idiot from northern Europe

Re: Error: Attempting to reference a deleted function

Post by Yumtard » February 17th, 2017, 5:16 pm

Game::Game( MainWindow& wnd )
:
wnd( wnd ),
gfx( wnd ),
player1 = box(100, 100, 255, 125, 125)
{
}

like this

and in game header just
box player1;

User avatar
Yumtard
Posts: 575
Joined: January 19th, 2017, 10:28 pm
Location: Idiot from northern Europe

Re: Error: Attempting to reference a deleted function

Post by Yumtard » February 17th, 2017, 5:26 pm

no here actually

void box::Move(MainWindow wnd, int screenWidth, int screenHeight)

try

void box::Move(MainWindow& wnd, int screenWidth, int screenHeight)

cameron
Posts: 794
Joined: June 26th, 2012, 5:38 pm
Location: USA

Re: Error: Attempting to reference a deleted function

Post by cameron » February 17th, 2017, 5:26 pm

Havent checked out new framework yet but(well I remember seeing chili post smth in a thread at one point, dk git url for new one), I don't think MainWindow has a copy constructor. Better pass that MainWindow as a ref.(in CalcVelocity)

The implicitly-declared or defaulted copy constructor for class T is defined as deleted if any of the following conditions are true: T has non-static data members that cannot be copied (have deleted, inaccessible, or ambiguous copy constructors);
Computer too slow? Consider running a VM on your toaster.

albinopapa
Posts: 4373
Joined: February 28th, 2013, 3:23 am
Location: Oklahoma, United States

Re: Error: Attempting to reference a deleted function

Post by albinopapa » February 17th, 2017, 7:29 pm

"attempting to reference a deleted function"

This usually happens when you try using the default, copy or move constructors when they have been implicitly or explicity deleted. For instance, the MainWindow class has this explicitly declared

Code: Select all

	MainWindow( const MainWindow& ) = delete;
	MainWindow& operator=( const MainWindow& ) = delete;
What this means is that if you are passing the 'Game::wnd' variable by value, the compiler will want to try and copy it, but since these two functions have been deleted, you are restricted from doing so. The best thing to do is to pass the wnd object by reference (&).

Code: Select all

// These two declarations and their corresponding definitions in the .cpp files 
void Move(MainWindow, int, int);
void CalcVelocity(MainWindow);

// Should be changed to take references to the MainWindow as Yumtard and cameron have said already.
void Move(MainWindow &, int, int);
void CalcVelocity(MainWindow &);
If you think paging some data from disk into RAM is slow, try paging it into a simian cerebrum over a pair of optical nerves. - gameprogrammingpatterns.com

User avatar
chili
Site Admin
Posts: 3948
Joined: December 31st, 2011, 4:53 pm
Location: Japan
Contact:

Re: Error: Attempting to reference a deleted function

Post by chili » February 18th, 2017, 4:39 am

Yes Papa is right. Usually that error (attempting to reference a deleted function) means you're trying to copy something that isn't copyable, or default construct something that isn't default constructable.

Objects like gfx and wnd represent the graphics device and the main window respectively. There can only be one of these in the framework, so it makes no sense to be able to copy the objects. So when you want to pass those into functions, you don't pass them by value (copying the object and making a new one thereby), you pass by reference (essentially giving the function the address of the already existing object).
Chili

turbosheep4
Posts: 2
Joined: February 17th, 2017, 4:51 pm

Re: Error: Attempting to reference a deleted function

Post by turbosheep4 » February 18th, 2017, 5:59 pm

Thanks for the quick replies and great explanations everyone, it's working now and it makes sense to me now B)

Post Reply