C ++:比较3个双精度值(明显为false时)返回true + return语句崩溃

时间:2019-12-05 10:05:40

标签: c++ vector double

所以我有这个函数,它假设仅使用代表其“角”的3个点来返回三角形的面积。在函数中,我必须检查所有点是否都具有相同的X或Y值(因为它不是三角形)

这是函数本身:

double Triangle::getArea() const 
{
    if (_points[0].getX() == _points[1].getX() == _points[2].getX() || _points[0].getY() == _points[1].getY() == _points[2].getY())
    {
        std::cout << "Error: X values or Y values of all the points are equal";
        _exit(0);
    }
    return 0.5 * (_points[0].getX() * (_points[1].getY() - _points[2].getY()) + _points[1].getX() * (_points[0].getY() - _points[3].getY()) + _points[2].getX() * (_points[0].getY() - _points[1].getY()));
}

三角形是一个类,它继承了一个包含所有点(std :: vector)的“ Point”类的向量,而“ Point”类则包含该点的X和Y值。

当我尝试将此指向函数时出现问题:

Point point1(0, 0);
Point point2(5, 0);
Point point3(0, 6);

我还注意到,当我将第三个点更改为(5,0)时,该函数不会引发异常,但是它在return语句中崩溃,错误为“向量下标超出范围”

这是三角形的基类,称为“多边形”,可创建点矢量:

#pragma once
#include "Shape.h"
#include "Point.h"
#include <vector>

class Polygon : public Shape
{
protected:
    std::vector<Point> _points;

public:
    Polygon(const std::string& type, const std::string& name) : Shape(name, type)
    {
    }
    virtual ~Polygon()
    {
        std::vector<Point>().swap(_points);
    }

};

很抱歉,这个问题很长,但是这个问题使我困扰了很长时间。而且我不太确定为什么会发生这种情况,我已经尝试并阅读了有关Vectors用法的更多信息,但是我没有发现我的函数或Vector实现有问题。

感谢所有愿意提供帮助的人! :D

编辑:发现了问题,我像往常一样傻了。检查解决的第一条评论

0 个答案:

没有答案
相关问题