简体   繁体   English

使用结构时的编译器错误

[英]Compiler Error when using a struct

I'm getting a strange compiler error initializing a struct.我在初始化结构时遇到了一个奇怪的编译器错误。

#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
#include <string.h>

struct RadarData
{
    unsigned int messageID : 32;
    unsigned int time : 32;
    float az;
    float el;
};
struct RadarData sendData;

sendData.az = 25;
sendData.el = 10;
sendData.messageID = 1;
sendData.time = 100;

This looks fine to me according to a few different tutorials, but on two different machines, I'm getting the following error when compiling:根据一些不同的教程,这对我来说看起来不错,但是在两台不同的机器上,编译时出现以下错误:

testserver.c:15:9: error: expected '=', ',', ';', 'asm' or ' attribute ' before '.' testserver.c:15:9: 错误: '=', ',', ';', 'asm' or ' attribute ' before '.' token令牌
testserver.c:16:9: error: expected '=', ',', ';', 'asm' or ' attribute ' before '.' testserver.c:16:9: 错误: '=', ',', ';', 'asm' or ' attribute ' before '.' token令牌
testserver.c:17:9: error: expected '=', ',', ';', 'asm' or ' attribute ' before '.' testserver.c:17:9: 错误: '=', ',', ';', 'asm' or ' attribute ' before '.' token令牌
testserver.c:18:9: error: expected '=', ',', ';', 'asm' or ' attribute ' before '.' testserver.c:18:9: 错误: '=', ',', ';', 'asm' or ' attribute ' before '.' token令牌

Why am I getting this error?为什么我会收到此错误?

sendData.az = 25;

Statements like this must be inside a function.像这样的语句必须在 function 中。 If you want to initialize the struct, there's a different syntax for that:如果要初始化结构,则有不同的语法:

struct RadarData sendData = { 25, 10, 1, 100 };

If I'm looking at your code right (and that's the complete relevant code), then you're placing statements outside of a function.如果我正在查看您的代码(这是完整的相关代码),那么您将语句放在 function 之外。 That's not right.那是不对的。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM