Collectives™ on Stack Overflow
Find centralized, trusted content and collaborate around the technologies you use most.
Learn more about Collectives
Teams
Q&A for work
Connect and share knowledge within a single location that is structured and easy to search.
Learn more about Teams
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
–
–
–
–
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.
–
–
–
–
_date(int day = 0, int month = 0, int year = 2020);
_date(int day, int month);
bool check_date_validity();
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.