Visual Studio 2019正在引发有关具有没有默认值的变量的结构的警告

问题描述 投票:0回答:1

采用这种结构定义:

typedef struct tagReferenceMWB
{
    COleDateTime datStartDate;
    COleDateTime datEndDate;
    COleDateTime datFirstMonday;
    COleDateTime datLastMonday;
    CString      strFileMWB;
    WORD         wCount;
    bool         bReferenced;
    bool         bModified;

} S_REFERENCE_MWB;

VS2019中的编译器发出警告:

Warning

Warning   C26495  Variable 'CChristianLifeMinistryDefines::tagReferenceMWB::bModified' is uninitialized. Always initialize a member variable (type.6).    Meeting Schedule Assistant  d:\my programs\2019\meetschedassist\meeting schedule assistant\christianlifeministrydefines.h   348 
Warning   C26495  Variable 'CChristianLifeMinistryDefines::tagReferenceMWB::bReferenced' is uninitialized. Always initialize a member variable (type.6).  Meeting Schedule Assistant  d:\my programs\2019\meetschedassist\meeting schedule assistant\christianlifeministrydefines.h   348 
Warning   C26495  Variable 'CChristianLifeMinistryDefines::tagReferenceMWB::wCount' is uninitialized. Always initialize a member variable (type.6).       Meeting Schedule Assistant  d:\my programs\2019\meetschedassist\meeting schedule assistant\christianlifeministrydefines.h   348 

由于我们无法在结构定义中分配默认值,我们如何解决问题?

Update

根据提供的答案中的注释,我检查了我的项目设置:

C++ Language

我的项目在十多年前开始生效,这个设置似乎是空的。我不知道这个留空的意义是什么?这些是我的选择:

C++ Language Options

将值更改为C ++ 14或C ++ 17会产生什么重大影响? IE浏览器。它会影响它可以运行的操作系统吗?

mfc visual-studio-2019
1个回答
3
投票

在C ++中,structclass相同(默认访问修饰符除外)。您可以通过提供默认构造函数来指定默认值。如果您使用的是C ++ 11,则还可以为非静态非const类成员使用类内初始值设定项,例如:

typedef struct tagReferenceMWB
{
    // ...
    WORD         wCount{ 0 };
    bool         bReferenced{ false };
    bool         bModified{ false };

} S_REFERENCE_MWB;

要么

typedef struct tagReferenceMWB
{
    // ...
    WORD         wCount{};
    bool         bReferenced{};
    bool         bModified{};

} S_REFERENCE_MWB;
© www.soinside.com 2019 - 2024. All rights reserved.