2012-03-25 153 views
0

我得到這個錯誤,當我試圖編譯我的代碼「要求作爲轉讓的左操作數左值」:Arduino的代碼編譯錯誤:

lvalue required as left operand of assignment.

的代碼是雖然模擬端口的按鈕閱讀。這是錯誤的是(空隙(循環)):

while (count < 5){ 
    buttonPushed(analogPin) = tmp; 

     for (j = 0; j < 5; j++) { 
       while (tmp == 0) { tmp = buttonPushed(analogPin); }     //something wrong with the first half of this line! 

     if(sequence[j] == tmp){ 
         count ++; 
       } 

     else { 
      lcd.setCursor(0, 1); lcd.print("Wrong! Next round:");      delay(1000); 
         goto breakLoops; 
       } 

     } 
} 

breakLoops: 
elapsedTime = millis() - startTime; 

在最高層,我有:int tmp;

回答

2
buttonPushed(analogPin) = tmp; 

此行不起作用。 buttonPushed是一個函數,只能從analogPin讀取;你不能指定C中函數的結果。我不確定你想要做什麼,但我想你可能是想用另一個變量來代替。

2

你有這樣一行:

 buttonPushed(analogPin) = tmp; 

您可能要改爲:

 tmp = buttonPushed(analogPin); 

隨着賦值運算符,在=操作左側的對象獲取上的權值=運營商,而不是相反。

+0

非常感謝Ouah某個位置分配的代碼。這似乎是完美的。 :-) – user1291351 2012-03-25 18:00:52

+0

@ user1291351不客氣! – ouah 2012-03-25 18:17:41

0

這裏的問題是你正試圖分配給臨時/右值。 C中的賦值需要左值。我猜你的buttonPushed功能的簽名實質上包含以下

int buttonPushed(int pin); 

這裏buttonPushed函數返回找到的按鈕,這沒有任何意義分配到的副本。爲了返回實際的按鈕與副本,你需要使用一個指針。

int* buttonPushed(int pin); 

現在,您可以進行以下

int* pTemp = buttonPushed(analogPin); 
*pTemp = tmp; 

這裏的任務是到這是一個左值,將是法律

+0

我不相信'buttonPressed'會因爲這行:'tmp = buttonPushed(analogPin);'在後面的代碼中返回任何類型的'struct'。可能無法修改其定義。 – Ryan 2012-03-25 15:14:08

+0

@minitech你是對的,看起來更可能是一個數值 – JaredPar 2012-03-25 15:17:20