【问题标题】:Arduino code compile error: "lvalue required as left operand of assignment"Arduino代码编译错误:“左值需要作为赋值的左操作数”
【发布时间】:2012-04-09 07:11:58
【问题描述】:

当我尝试编译我的代码时出现此错误:

需要左值作为赋值的左操作数。

代码正在通过模拟端口读取按钮。这是错误所在(在 void(loop) 中):

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;

【问题讨论】:

    标签: c loops while-loop arduino


    【解决方案1】:

    这里的问题是你试图分配一个临时/右值。 C 中的赋值需要一个左值。我猜你的buttonPushed 函数的签名基本上如下

    int buttonPushed(int pin);
    

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

    int* buttonPushed(int pin);
    

    现在你可以让你的分配代码如下

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

    这里的赋值是一个左值并且合法的位置

    【讨论】:

    • 我不相信 buttonPushed 会返回任何类型的 struct 因为这条线:tmp = buttonPushed(analogPin); 稍后在代码中。可能无法修改其定义。
    • @minitech 你是对的,看起来更像是一个数值
    【解决方案2】:

    你有这行:

         buttonPushed(analogPin) = tmp;
    

    你可能想要:

         tmp = buttonPushed(analogPin);
    

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

    【讨论】:

    • 非常感谢 Ouah。这似乎很完美。 :-)
    【解决方案3】:
    buttonPushed(analogPin) = tmp;
    

    这条线不工作。 buttonPushed是一个函数,只能从analogPin读取;您不能在 C 中分配函数的结果。我不确定您要做什么,但我认为您可能打算使用另一个变量。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-12-20
      • 2011-03-04
      • 2017-01-10
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多