为什么我在 Visual C 中收到这些警告2008年什么时候构建结构?
我有这段代码
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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
这正是编译器所说的:它无法为结构生成默认构造函数或赋值运算符,因为它有一个 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 areconst
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 withstatic const fooStruct foo[] = ...
, the extraconst
onfooBool
is pointless.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.
此外,如果您进行部分初始化,则 MSVC2008 将引发错误(与 MSVC2010 一样),这是 C++03 和 C++11 定义的不正确行为。我在堆栈溢出的另一个线程中发布了更多相关内容,您可以阅读此处
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
MSVC will throw an error on this code along with the warnings you mentioned.