添加链接
link之家
链接快照平台
  • 输入网页链接,自动生成快照
  • 标签化管理网页链接
相关文章推荐
坏坏的保温杯  ·  从根本上解决npm ...·  1 月前    · 
谈吐大方的鸵鸟  ·  Powershell/Vba ...·  1 年前    · 
追风的凳子  ·  update ...·  1 年前    · 
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
                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, 2020 at 1:40
                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, 2020 at 1: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, 2020 at 1:52
                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, 2020 at 2:00

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.

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, 2020 at 1:54 @PratapBiswakarma: the three-argument constructor (with defaults) will do that, and more. – paxdiablo Sep 13, 2020 at 1:58 @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, 2020 at 2:00 I keep that one bookmarked: What are the rules about using an underscore in a C++ identifier? – user4581301 Sep 13, 2020 at 2:02 _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.