2017-04-21 53 views
0
typedef struct {int a; int b;} A_t; 
A_t AA; 
AA.a = 3; AA.b = 4; 
// compilation fails here 
A_t& BB = AA; 

當試圖建立到現有結構的參考,我得到以下編譯錯誤: 「預期標識符或‘(’前' &「令牌」結構引用編譯錯誤:預期標識符或「(」前「和」令牌

我缺少什麼?

+5

這顯然是C++,所以刪除C標記。並用C++編譯器編譯 – Jonas

+0

C和C++是完全不同的語言。爲標籤選擇一個。 – tambre

+2

C標籤應該保留,因爲C和C++之間的混淆是問題的根源。 – dbush

回答

3

你是一個C編譯器,而不是C++編譯器編譯。

C沒有引用的概念,所以聲明一個變量一樣A_t &BB是無效的語法。

如果您使用的是引用,則需要使用C++編譯器進行編譯。

0

如果你正在寫一個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! 
相關問題