代码在一个Visual Studio C ++项目中工作,但在另一个项目中不工作

时间:2015-05-31 07:04:29

标签: visual-c++ compiler-errors

以下是相关代码。

uint16_t get_unaligned_16(uint16_t *ptr)
{
#pragma pack(push, 1)
    struct packed {
      uint16_t __v;
    } *__p = (packed *)(ptr);
#pragma pack(pop)
    return __p->__v;
}

两个项目都针对相同的VC ++运行时(2013)。

以下是编译器的确切错误。

error C2065: 'packed' : undeclared identifier error C2059: syntax error : ')' 
error C2061: syntax error : identifier 'get_unaligned_32' 
error C2059: syntax error : ';' error C2059: syntax error : 'type' 
error C2065: 'packed' : undeclared identifier error C2059: syntax error : ')' 
warning C4013: 'get_unaligned_32' undefined; assuming extern returning int

为什么这不能在一个项目中编译,而在另一个项目中编译的任何想法?

picture of code that doesn't compile

1 个答案:

答案 0 :(得分:2)

在C中,必须使用带有结构名称的单词structstruct packed是要定义的结构的名称。在C ++中,您可以省略您在此处执行的关键字,仅在(packed *)的演员表中将其引用为ptr。我猜测给出错误的文件是.c文件,或编译为C。