【发布时间】: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++