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
}
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);
/********************************/
};
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;
}
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);
/****************************************/
};
Thanks in advance )