为什么我在 Visual C 中收到这些警告2008年什么时候构建结构?

发布于 2024-11-30 14:20:38 字数 653 浏览 5 评论 0原文

我有这段代码

typedef struct
{
    const char* fooString;
    const bool  fooBool;
}fooStruct;

和这个初始化程序:

static const fooStruct foo[] =
{
    {"file1", true},
    {"file2", false},
    ....
};

使用这段代码,我在 VS2008 中有 3 个警告:

error C2220: warning treated as error - no 'object' file generated  
warning C4510: '<unnamed-tag>' : default constructor could not be generated
warning C4512: '<unnamed-tag>' : assignment operator could not be generated
warning C4610: struct '<unnamed-tag>' can never be instantiated - user defined constructor required 

I have this code

typedef struct
{
    const char* fooString;
    const bool  fooBool;
}fooStruct;

And this initializer:

static const fooStruct foo[] =
{
    {"file1", true},
    {"file2", false},
    ....
};

With this code I have 3 warnings in VS2008:

error C2220: warning treated as error - no 'object' file generated  
warning C4510: '<unnamed-tag>' : default constructor could not be generated
warning C4512: '<unnamed-tag>' : assignment operator could not be generated
warning C4610: struct '<unnamed-tag>' can never be instantiated - user defined constructor required 

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

若有似无的小暗淡 2024-12-07 14:20:38

这正是编译器所说的:它无法为结构生成默认构造函数或赋值运算符,因为它有一个 const 成员 (const bool fooBool)。 const 或引用的结构成员不能默认初始化,因此必须在用户编写的构造函数或赋值运算符中显式初始化它们。

一种解决方案是编写自己的默认构造函数和赋值运算符(并符合三的规则< /a>,您还应该编写一个复制构造函数;析构函数不是绝对必要的,但这是一个很好的做法)。另一种更简单的解决方案是使 fooBool 成为非 const。然后,编译器会很乐意为您生成默认的构造函数和赋值运算符。

由于您已经使用 static const fooStruct foo[] = ... 创建了这些 const 实例的数组,因此额外的 const fooBool 毫无意义。

It's exactly what the compiler says: it can't generate a default constructor or assignment operator for your struct because it has a const member in it (const bool fooBool). struct members which are const or which are references cannot be default-initialized, so they must be explicitly initialized in a user-written constructor or assignment operator.

One solution is to write your own default constructor and assignment operator (and in line with the rule of three, you should also write a copy constructor; a destructor isn't strictly necessary but is good practice). The alternative, easier solution is just to make fooBool non-const. Then, the compiler will happily generate the default constructor and assignment operator for you.

Since you're already creating an array of const instances of these with static const fooStruct foo[] = ..., the extra const on fooBool is pointless.

嘿咻 2024-12-07 14:20:38

C4610 警告不正确。这是 Visual C++ 中的一个已知错误。请参阅 Microsoft Connect bug “C4610 发行不当”。 < /一>

<一href="https://stackoverflow.com/questions/7151298/c-warnings-on-vs2008/7151406#7151406">Adam Rosenfield 解释了为什么发出其他两个警告(C4510 和 C4512)。

The C4610 warning is incorrect. This is a known bug in Visual C++. See the Microsoft Connect bug "Improper issuance of C4610."

Adam Rosenfield explains why the other two warnings (C4510 and C4512) are emitted.

青柠芒果 2024-12-07 14:20:38

此外,如果您进行部分初始化,则 MSVC2008 将引发错误(与 MSVC2010 一样),这是 C++03 和 C++11 定义的不正确行为。我在堆栈溢出的另一个线程中发布了更多相关内容,您可以阅读此处

// Partial initialization, leaving it to the compiler
// to do aggregate value-initialization
fooStruct foo ={"file1", /*missing true/false, compiler should set false*/ };

MSVC 会在此代码上抛出错误你提到的警告。

Also if you do partial initialization then MSVC2008 will throw errors (as does MSVC2010) which is incorrect behavior as defined by C++03 and C++11. I posted more on this in another thread on stack overflow which you can read here

// Partial initialization, leaving it to the compiler
// to do aggregate value-initialization
fooStruct foo ={"file1", /*missing true/false, compiler should set false*/ };

MSVC will throw an error on this code along with the warnings you mentioned.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文