为什么我不能对另一个类的 static char[] 执行 sizeof ?
为什么下面的代码会产生编译错误?
编辑:我的原始代码不清楚 - 我已将代码分成单独的文件...
First.h
class First
{
public:
static const char* TEST[];
public:
First();
};
First.cpp
const char* First::TEST[] = {"1234", "5678"};
First::First()
{
uint32_t len = sizeof(TEST); // fine
}
确定 First
类中的大小似乎没问题,但是...
Second.h
class Second
{
public:
Second();
};
Second.cpp
#include "First.h"
Second::Second()
{
uint32_t len = sizeof(First::TEST); // error
uint32_t elements = (sizeof(First::TEST) / sizeof(First::TEST[0])); // error
}
我收到以下错误: 'const char *[]':操作数大小非法
Why does the following code generate a compile error?
Edit: My original code wasn't clear - I've split the code up into separate files...
First.h
class First
{
public:
static const char* TEST[];
public:
First();
};
First.cpp
const char* First::TEST[] = {"1234", "5678"};
First::First()
{
uint32_t len = sizeof(TEST); // fine
}
Determining the size within the First
class seems fine, however...
Second.h
class Second
{
public:
Second();
};
Second.cpp
#include "First.h"
Second::Second()
{
uint32_t len = sizeof(First::TEST); // error
uint32_t elements = (sizeof(First::TEST) / sizeof(First::TEST[0])); // error
}
I get the following error: 'const char *[]': illegal sizeof operand
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
sizeof 仅适用于完整类型。
const char* TEST[]
在 First.cpp 中定义之前并不是一个完整的类型。要使其按您希望的方式工作,您可以:
static const char* TEST[2];
)First::TEST
的大小。该方法不能是内联的,它必须在 First.cpp 中定义。sizeof only works on complete types.
const char* TEST[]
is not a complete type until it is defined in First.cpp.To get this to work as you wish, you can:
First::TEST
array to its declaration (static const char* TEST[2];
)First::TEST
. The method cannot be inline, it would have to be defined in First.cpp.主要是因为First.cpp和Second.cpp的编译是相互独立的。
Second 中的 sizeof() (通常)在编译时解析,此时仅知道数组的声明,并且无法计算静态
尚未分配的空间。请参阅http://en.wikipedia.org/wiki/Sizeof#sizeof_and_incomplete_typesPrimarily because the compilation of First.cpp and Second.cpp are independent of each other.
sizeof() in Second is (generally) resolved at compile time, when only the array's declaration is known, and space for the static
hasn't been allocatedcannot be calculated. See http://en.wikipedia.org/wiki/Sizeof#sizeof_and_incomplete_types当您展开 Second.cpp 时,它会变得更加清晰。这是正在编译的整个内容(1 个编译单元):
如果您看这里,First::TEST 显然没有大小,并且 sizeof(FIRST::TEST) 毫无意义。
为什么不直接有一个返回 TEST 长度的方法呢?
It gets clearer when you expand Second.cpp. This is the whole thing that is being compiled (1 compilation unit):
If you look here, First::TEST clearly has no size, and sizeof(FIRST::TEST) is meaningless.
Why not just have a method that returns the length of TEST?
对我来说,即使第一个
sizeof(TEST)
也无法编译,因为 TEST 尚未声明大小(仅在链接时解析)。如果我将
TEST[]
更改为TEST[10]
,则两种情况都会编译。这两个结果都是我所期望的。
您使用哪个编译器?
For me even the first
sizeof(TEST)
fails to compile as TEST has not been declared with a size (that is only resolved at link time).If I change
TEST[]
to, say,TEST[10]
, then both cases compile.Both results are what I would expect.
Which compiler are you using?