Error code C26495

The Partridge Family were neither partridges nor a family. Discuss.
Post Reply
Lividpayment
Posts: 10
Joined: April 13th, 2020, 8:47 pm

Error code C26495

Post by Lividpayment » May 14th, 2020, 3:10 pm

Game.H

Code: Select all

#pragma once

#include "Keyboard.h"
#include "Mouse.h"
#include "Graphics.h"
#include "Player.h"
#include "Platform.h"
#include <random>
#include "Item.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              */
    /********************************/
    std::random_device rd;
    std::mt19937 rng;
    std::uniform_int_distribution<int> xDistPlatform;
    std::uniform_int_distribution<int> yDistPlatform;
    std::uniform_int_distribution<int> xDistItem;
    std::uniform_int_distribution<int> yDistItem;
    bool IsEaten = false;
    static constexpr int Nplatform = 10;
    Platform platform[Nplatform];
    item Item;
    Player player;
    bool GameHasStarted = true;

};
Game.CPP:
------------------------

Code: Select all

#include "MainWindow.h"
#include "Game.h"
#include <random>

Game::Game(MainWindow& wnd)
    :
    wnd(wnd),
    gfx(wnd),
    rng(rd()),
    xDistPlatform(0, 700),
    yDistPlatform(0, 590),
    xDistItem(0, 700),
    yDistItem(0, 590),
    Item(xDistItem(rng), yDistItem(rng), 1)

{
    for (int i = 0; i < Nplatform; i++)
    {
        platform[i].init(xDistPlatform(rng), yDistPlatform(rng));
    }

}
My error code:
(Severity Code Description Project File Line Suppression State
Warning C26495 Variable 'Game::platform' is uninitialized. Always initialize a member variable (type.6). Engine C:\USERS\USER\DESKTOP\CHILI DIRECTX FRAMEWORK (2)\CHILI DIRECTX FRAMEWORK (8)\CHILI FRAMEWORK 2016\ENGINE\GAME.CPP 25)


-----------------------------------------------------------------------

I am getting an C26495 intialization error for my platform class, I have checked my Game.H header and my order of intialization appears to be correct, so I think my problem lies in the constructor somewhere. Can somewhere have a look over and see if I declared anything wrong. Thanks!

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

Re: Error code C26495

Post by albinopapa » May 14th, 2020, 4:29 pm

First off, it looks like a warning so shouldn't be preventing compilation.

Second, Visual Studio 2019 has a lot of static analysis turned on and warns about unsafe practices.

I was going to say that Game::platform isn't being initialized using the initializer list, but it's an array. One problem that I see is

Code: Select all

for (int i = 0; i < Nplatform; i++)
{
    platform.init(xDistPlatform(rng), yDistPlatform(rng));
}
If anything, it should be

Code: Select all

for (int i = 0; i < Nplatform; i++)
{
    platform[i].init(xDistPlatform(rng), yDistPlatform(rng));
}
Just a suggestion not related to the error, but since Game::platform is an array, it would be more clear to name it something plural like Game::platforms. This way there is not confusion about whether it is a single object or an array of objects. Using a ranged based for loop, you could initialize the Platform objects like so:

Code: Select all

for (Platform& platform : platforms )
{
    platform.init(xDistPlatform(rng), yDistPlatform(rng));
}
and all you'd have to change is Game::platform[Nplatforms] to Game::platforms[NPlatforms] and the for loop declaration.
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

Post Reply