c++声明字节数组时出现语法错误

发布于 2024-11-26 18:02:56 字数 803 浏览 4 评论 0原文

我试图声明一个字节数组,以便我可以遍历它们并单独使用它们中的每一个。这是数组

const BYTE keybyte[] = {
    0x41, 0x42, 0x43, 0x44, 0x45, 0x46, 0x47, 0x48,
    0x49, 0x4A, 0x4B, 0x4C, 0x4D, 0x4E, 0x4F, 0x50,
    0x51, 0x52, 0x53, 0x54, 0x55, 0x56, 0x57 0x58,
    0x59, 0x5A, 0x30, 0x31, 0x32, 0x33, 0x34, 0x35,
    0x36, 0x37, 0x38, 0x39, 0x25, 0x27, 0x26, 0x28,
    0x0D, 0x20, 0x10, 0x11, 0x12, 0x18};

出于某种原因,当我编译时,它给了我这些错误:/

error C2143: syntax error : missing '}' before 'constant'
error C2143: syntax error : missing ';' before 'constant'
error C2059: syntax error : 'constant'
error C2143: syntax error : missing ';' before '}'
error C2059: syntax error : '}'

我不明白,因为如果我用

const BYTE keybyte[] = {0,0,0,0};

它替换它,它就可以完美地工作,没有错误:/?

Im trying to declare an array of bytes so I can go through them and use each one of them seperatly. This is the array

const BYTE keybyte[] = {
    0x41, 0x42, 0x43, 0x44, 0x45, 0x46, 0x47, 0x48,
    0x49, 0x4A, 0x4B, 0x4C, 0x4D, 0x4E, 0x4F, 0x50,
    0x51, 0x52, 0x53, 0x54, 0x55, 0x56, 0x57 0x58,
    0x59, 0x5A, 0x30, 0x31, 0x32, 0x33, 0x34, 0x35,
    0x36, 0x37, 0x38, 0x39, 0x25, 0x27, 0x26, 0x28,
    0x0D, 0x20, 0x10, 0x11, 0x12, 0x18};

For some reason when I compile it gives me these errors :/

error C2143: syntax error : missing '}' before 'constant'
error C2143: syntax error : missing ';' before 'constant'
error C2059: syntax error : 'constant'
error C2143: syntax error : missing ';' before '}'
error C2059: syntax error : '}'

I dont understand because if I replace it with

const BYTE keybyte[] = {0,0,0,0};

it works perfectly fine with no errors :/?

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

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

发布评论

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

评论(2

甲如呢乙后呢 2024-12-03 18:02:56

0x570x58 之间缺少逗号。

You are missing a comma between 0x57 and 0x58.

梦萦几度 2024-12-03 18:02:56

亚历山大·盖斯勒是对的——缺少逗号。下次尝试使用 gcc 和 clang 编译文件。它将引导您解决问题:

对于文件 test.h 包含:

const char keybyte[] = {0x41, 0x42, 0x43 0x44, 0x45, 0x12, 0x18};

当我尝试 GCC 时:

test.h:1:42: error: expected ‘}’ before numeric constant

编译器解析器说,第 1 行和字符 42 上的语法错误:

当我询问 clang 时,我得到更好的输出:

clang test.h
test.h:1:42: error: expected '}'
const char keybyte[] = {0x41, 0x42, 0x43 0x44, 0x45, 0x12, 0x18};
                                         ^

所以我看看问题出在哪里。

Alexander Gessler is right - comma is missing. Next Time try compile file with gcc and clang. It will guide you to problem resolution:

For file test.h containing:

const char keybyte[] = {0x41, 0x42, 0x43 0x44, 0x45, 0x12, 0x18};

when I try GCC:

test.h:1:42: error: expected ‘}’ before numeric constant

Compilers parser says, the syntax is wrong on the line 1 and the character 42:

When I ask clang, I get even better output:

clang test.h
test.h:1:42: error: expected '}'
const char keybyte[] = {0x41, 0x42, 0x43 0x44, 0x45, 0x12, 0x18};
                                         ^

so I see where the problem is.

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