-1

I have a code that seems to be error free but I am getting this warning C26495: Variable '_date::year' is uninitialized. Always initialize a member variable (type.6). It also says +3 overloads for the constructor in the same warning. Why am i getting this warning. I have looked online but cannot figure out what is the cause of this. It seems to be related to the constructor. Do i need to declare additional constructors or change the signature of constructor? here is my code

_date.h header file

#pragma once

#include <iostream>

using namespace std;


//definition for class date
class _date
{
    int day;
    int month;
    int year;

public:
    _date(int day = 0, int month = 0, int year = 2020);
    _date(int day, int month);
    bool check_date_validity();

};

_date.cpp file

#include "_date.h"


_date::_date(int day_value, int month_value) : day { day_value }, month{ month_value }
{   
}

// and additional function definitions
  • 2
    You don't initialize the `year` member variable. This leaves you with a somewhat unstable date object: You have no idea what year you will get. – user4581301 Sep 13 '20 at 01:40
  • 1
    are you expecting the first constructor to do the initialization for you, thats not what that syntax means. You must assign values to each member field – pm100 Sep 13 '20 at 01:44
  • i thought if i have a default value for each member in the constructor where i wrote `year=2020` . isn't it supposed to pick up that default value from that prototype? even if i set `year{2020}1 in the initialization list the warning doesn't go away. – Pratap Biswakarma Sep 13 '20 at 01:52
  • @pm100 what is the syntax can you explain? – Pratap Biswakarma Sep 13 '20 at 01:59
  • 2
    You don't *need* the two-argument constructor when you have a three-argument one with a default value for the third argument. – paxdiablo Sep 13 '20 at 02:00

2 Answers2

1

Your particular issue is that, in the day/month constructor, the year member variable is not being initialised so may be set to some arbitrary value.


You have a constructor like (and note the changes to the default values, there's no such thing as the zeroth day of the month or month of the year so, unless you're using some non-human encoding (like struct tm, tm_mon), it's probably better to use 1):

_date(int day = 1, int month = 1, int year = 2020);

With that constructor in existence, there's probably no need for the day/month one since calling the day/month/year one without a year, will default the year to the non-arbitrary value of 2020, solving that warning. That means only one constructor, something like:

_date::_date(int dayVal, int monthVal, int yearVal)
  : day   (dayVal)
  , month (monthVal)
  , year  (yearVal)
{}

As an aside, I'm also wary of the wisdom of starting class names (or any variables, really) with an underbar. That may be reserved for the implementation ("may" because I'm too tired to look into it at the moment but I thought I'd at least bring it to your notice, though some very helpful person provided a link in the comments).

Also, it's not wise to do using namespace std in a header file. I won't explain why here since there are far better answers on that point elsewhere on SO.

paxdiablo
  • 854,327
  • 234
  • 1,573
  • 1,953
  • i am using another function to initialize instances of `_date` class to with two arguments. i want to set `year` to 2020 default. so i need the two argument constructor. – Pratap Biswakarma Sep 13 '20 at 01:54
  • 1
    @PratapBiswakarma: the three-argument constructor (with defaults) will do that, and more. – paxdiablo Sep 13 '20 at 01:58
  • 1
    @PratapBiswakarma If you want `_date(int day_value, int month_value)` to set to default to year to 2020, then you should write it to actually assign that value to `year`. The existence of other constructors is irrelevant at that point since they aren't being invoked when `_date(int day_value, int month_value)` is invoked. – TheUndeadFish Sep 13 '20 at 02:00
  • 2
    I keep that one bookmarked: [What are the rules about using an underscore in a C++ identifier?](https://stackoverflow.com/questions/228783/what-are-the-rules-about-using-an-underscore-in-a-c-identifier) – user4581301 Sep 13 '20 at 02:02
  • Awesome, @user4581301, brought that into the answer, hope you don't mind. – paxdiablo Sep 13 '20 at 02:08
1

depending on the version of c++ you are using you may be able to do this

#pragma once

#include <iostream>

using namespace std;


//definition for class date
class _date
{
    int day;
    int month;
    int year = 2020; <=====================================

public:
    _date(int day = 0, int month = 0, int year = 2020);
    _date(int day, int month);
    bool check_date_validity();

};
pm100
  • 48,078
  • 23
  • 82
  • 145