c语言 - 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;

buttonPushed(analogPin) = tmp;

这条线不管用。CCD_ 2是一个函数,只能从CCD_;你不能给C中函数的结果赋值。我不确定你想做什么,但我认为你可能想用另一个变量代替。

您有这行:

     buttonPushed(analogPin) = tmp;

你可能想要:

     tmp = buttonPushed(analogPin);

使用赋值运算符时,=运算符左侧的对象将获得=运算符右侧的值,而不是相反的值。

这里的问题是您试图分配给一个临时的/rvalue。C中的赋值需要一个左值。我猜您的buttonPushed函数的签名本质上是以下

int buttonPushed(int pin);

这里的buttonPushed函数返回一个已找到按钮的副本,但分配给它没有意义。为了返回实际按钮与副本,您需要使用指针。

int* buttonPushed(int pin);

现在,您可以将您的分配代码设置为以下

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

在这里,赋值被分配到一个位置,该位置是一个左值,并且将是合法的

相关内容

最新更新