结构引用编译错误:预期的标识符或"&"标记之前的"("

  • 本文关键字:标识符 编译 引用 错误 结构 c++
  • 更新时间 :
  • 英文 :

typedef struct {int a; int b;} A_t;
A_t AA;
AA.a = 3; AA.b = 4;
// compilation fails here
A_t& BB = AA;

尝试创建对现有结构的引用时,我会收到以下汇编错误:"预期标识符或'('''&'token"

我缺少什么?

您是用C编译器而不是C 编译器编译的。

c没有参考的概念,因此声明诸如 A_t &BB之类的变量是无效的语法。

如果您使用的是引用,则需要使用C 编译器进行编译。

如果您正在编写C 程序,那么您就可以做

struct A_t{
int a;
int b;
};
A_t AA; // You don't need to preceed the struct name with the keyword struct
AA.a = 3;
AA.b = 4;
// your compilation failed in the below step
A_t& BB = AA; // Well, reference to variable (as in &BB) is a functionality of C++.
//If you get an error here, you're probably using a C compiler for a C++ pgm!

最新更新