Visual Studio 非法操作数的大小
我目前正在将Linux上开发的程序移植到Win32。在其他问题中,我有一个非常奇怪的问题。 头文件包含类似这样的内容:
namespace Networking {
struct MetaStruct
{
int iDataType;
int iDataSize;
void* pData;
};
const int MetaStructSize = sizeof(MetaStruct) - sizeof(MetaStruct::pData);
};
This在Linux上编译得很好,但是在使用VS2010编译Win32时出现此错误: Networking.hpp(50): error C2070: '': invalid sizeof operand
我尝试在 MetaStruct
之前添加 Networking::
但它没有'不要改变任何事情。奇怪的是,当我用鼠标悬停它时,VS2010 给出了 sizeof
的正确值,但不会编译它。为什么?
I am currently porting a program developed on Linux to Win32. Amongst other problems, I have one that is pretty weird.
A header file contains something like this:
namespace Networking {
struct MetaStruct
{
int iDataType;
int iDataSize;
void* pData;
};
const int MetaStructSize = sizeof(MetaStruct) - sizeof(MetaStruct::pData);
};
This compiles fine on fine on Linux, but I get this error when compiling for Win32 using VS2010:Networking.hpp(50): error C2070: '': illegal sizeof operand
I tried adding the Networking::
before MetaStruct
but it doesn't change anything. The weird thing is VS2010 gives me the correct value of the sizeof
when I hover it with the mouse, but won't compile it. Why?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
在 C++03 中有两种形式的 sizeof 表达式(参见 ISO/IEC 14882:2003 5.3.3 [expr.sizeof])。
MetaStruct::pData
既不是有效的表达式(解析为对象类型)也不是类型的名称。您必须执行
或
更新:感谢@hvd,他指出这现在在 C++11 中实际上应该是合法的。
现在,您可以使用一个id-表达式来引用类的非静态成员(在不对其求值的上下文中)。显然VS2010不支持这一点。
C++11 中还有一种新形式的
sizeof
:sizeof ... (identifier)
但这与这里无关。In C++03 There are two forms of sizeof expressions (see ISO/IEC 14882:2003 5.3.3 [expr.sizeof]).
MetaStruct::pData
is neither a valid expression (resolving to an object type) nor the name of a type.You would have to do
or
Update: Thanks to @hvd who points out that this should actually be legal in C++11 now.
You can now use an id-expression that refers to a non-static member of a class in contexts where it isn't evaluated. Evidently this isn't supported by VS2010.
There's also a new form of
sizeof
in C++11:sizeof ... ( identifier )
but that's not relevant here.