【问题标题】:Strange error: non-lvalue in assignment in C++奇怪的错误:C++ 中的非左值赋值
【发布时间】:2014-10-27 22:44:12
【问题描述】:

这是我的程序,可以找到矩形的面积、周长以及矩形是否为正方形。它通过使用两个坐标和建模结构来完成所有这些工作。我对 C++ 相当陌生,我在这一行遇到一个不熟悉的错误: if(abs(y-a2.y) = abs(a2.x - x) ) { 错误说:分配中的非左值。

#include <cstdlib>
#include <iostream>
#include <math.h>

/*
Name: Rectangle
Author: ------
Date: 27/10/14 04:31
Description: A program that finds the area, perimeter, and if a rectangle is square
*/

using namespace std;

class Point
{
private:
    int x, y;

public:

    Point ()
    {
        x = 0;
        y = 0;
    }

    //A four parameter constructor

    Point (int a, int b)
    {
        x = a ;
        y = b ;
    }

    //Setter function

    void setX (int a)
    {
        x = a ;
    }

    void setY (int b)
    {
        y = b ;
    }

    //Accessor functions

    int getX ()
    {
        return x ;
    }

    int getY ()
    {
        return y ;
    }

    //Function to print points

    void printPoint ()
    {
        cout << endl << "(" << x << ", " << y << ")" << endl ;
    }

    //Function to enter new points

    Point newPoint ()
    {
        Point aPoint;
        int a;
        int b;
        cout << "Enter first x coordinate: " ;
        cin >> a ;
        cout << "Enter first y coordinate: " ;
        cin >> b ;
        aPoint.setX(a);
        aPoint.setY(b);
        return aPoint;
    }

    //Function to find area

    int areaA (Point a2)
    {
        int height = y - a2.y ;
        int length = a2.x - x ;
        int area = abs((length * height)) ;
        return area ;
    }

    //Function to find perimeter

    int perimeterA (Point a2)
    {
        int height1 = y - a2.y ;
        int length1 = a2.x - x ;
        int perimeter1 = abs(((length1 + height1) * 2)) ;
        return perimeter1 ;
    }

    //Function to determine shape

    int isSquare (Point a2)
    {
        int square;
        if ( abs(y - a2.y) = abs(a2.x - x) )     //****ERROR ON THIS LINE****
        {
            cout << "It is square" ;
        }

        else
        {
            cout << "It is not square" ;
        }
        return square;
    }
};

Point newPoint();

int main(int argc, char *argv[])
{
    cout << "Enter top left coordinate first and bottom right coordinate second" ;
    cout << endl << endl ;
    Point firstPoint;
    Point secondPoint;
    int areaR;
    int perimeter;
    firstPoint = firstPoint.newPoint();
    secondPoint = secondPoint.newPoint();
    cout << endl ;
    cout << "First point: " ;
    firstPoint.printPoint();
    cout << "Second point: " ;
    secondPoint.printPoint();
    cout << endl ;

    areaR = firstPoint.areaA(secondPoint);
    cout << "Area: " << areaR << endl ;
    perimeter = firstPoint.perimeterA(secondPoint);
    cout << "Perimeter: " << perimeter << endl;

    cout << endl ;
    system("PAUSE");
    return EXIT_SUCCESS;
}

【问题讨论】:

  • 相等性由== 而非= 进行检查
  • "我在这一行收到一个不熟悉的错误:if(abs(y-a2.y) = abs(a2.x - x)) { 错误说明:非赋值中的左值。我不确定这是什么意思或如何解决它。"您是否在 "lvalue" 上进行了 Internet 搜索以了解该词的含义?
  • "lvalue required as left operand of assignment " error 的可能重复项(该问题针对 c,但在这种情况下规则相同)
  • 所以下一个要问自己的合乎逻辑的事情是“这是一项任务吗?它应该是吗?”。
  • @HostileFork: 真的很弯op=: coliru.stacked-crooked.com/a/71dfb0ed9bc208d6

标签: c++


【解决方案1】:
if(abs(y-a2.y) = abs(a2.x - x) )

尝试将abs(a2.x - x) 分配给abs(y-a2.y),这是没有意义的。使用== 比较值。

顺便说一句,isSquare 返回一个未初始化的值,这是一个错误。要么返回一些有意义的东西,使函数void

【讨论】:

    【解决方案2】:

    我在那个错误中没有看到任何奇怪的地方。该行确实尝试将某些内容分配给非左值,正如错误消息所述。相等比较运算符在 C++ 中拼写为 ==。表达式上下文中的单个= 是赋值运算符。

    还不清楚为什么在包含 C 库头文件时要混合两种不同的样式:&lt;cstdlib&gt;&lt;math.h&gt;。如果您决定包含&lt;cstdlib&gt;,那么包含&lt;cmath&gt; 会更有意义。请记住,尽管通过 &lt;c...&gt; 标头可用的标准库函数应被视为 std 命名空间的成员。现代 C++ 中已弃用包含 .h 版本的 heqaders。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2012-07-26
      • 2012-12-15
      • 1970-01-01
      • 1970-01-01
      • 2018-02-02
      • 1970-01-01
      • 2017-11-07
      • 2017-01-10
      相关资源
      最近更新 更多