Hello everyone,
I'm not a complete beginner in computers but I took a long break from them due to life's requirements including children etc...
Anyways, I'm getting into C++ programming and the way I learn is to jump into the deep end and try to stay afloat....
Here's a very simple program and very easy to set up and follow. When I run the program, using Visual Studio 2017 IDE, I get an error as follows...."Exception thrown: read access violation. this was nullptr."
Here is the code which comprises of: Main.cpp file and 2 classes called Game and GameObject with their corresponding cpp and h files.
So.….in main I create a Game object and call the objects runGame() method.
In runGame(), I create a GameObject object, then I invoke Game objects update() method which invokes GameObject objects objUpdate which performs xPos++ and thats where I get the error.
Hope I explained that ok...
Any help would really be appreciated and a big learning curve for me....I've exhausted the google for help but cant find a specific example and solution...
Thank you all!!
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116
|
Main.cpp
--------
#include "Game.h"
Game* game = new Game();
int main(int argc, char *argv[]) {
game->runGame();
delete game;
game = nullptr;
return 0;
}
------------------------------------------
Game.cpp
--------
#include "Game.h"
GameObject* player = nullptr;
void Game::runGame()
{
GameObject* player = new GameObject(5, 5);
update();
render();
delete player;
player = nullptr;
}
void Game::update()
{
player->objUpdate();
}
void Game::render()
{
}
------------------------------------------
Game.h
------
#pragma once
#include "GameObject.h"
class Game
{
public:
void runGame();
void update();
void render();
};
------------------------------------------
GameObject.cpp
--------------
#include "GameObject.h"
GameObject::GameObject(int x, int y)
{
xPos = x;
yPos = y;
}
GameObject::~GameObject()
{
}
void GameObject::objUpdate()
{
xPos++;
}
void GameObject::objRender()
{
}
------------------------------------------
GameObject.h
------------
#pragma once
class GameObject
{
public:
GameObject(int x, int y);
~GameObject();
void objUpdate();
void objRender();
public:
int xPos;
int yPos;
};
------------------------------------------
|