0

运行包含以下类的项目时出现运行时错误:
Shape-抽象
Polygon :public Shape-抽象
Triangle :public Polygon-普通类

我创建了一个vector<Shape*> shapes,当我在我的代码中尝试时:

shapes[i] = new Triangle(****);

我有运行时错误。
它与双重继承有关还是我的代码中有问题?
因为我也去上课了Circle :public Shape,什么时候做

shapes[i] = new Circle(*****);

一切正常..

编辑:
正如vector<Shape*> shapes;你们大多数人所猜测的那样。
我没有超出我已将其更改为的 voctor 的边界.push_back(new Circle()),它仍然只是崩溃。

我得到的运行时错误没有显示任何错误代码,它只是关闭了 exe 文件。

形状绝对是抽象的,因为我所有的方法都是纯虚拟 的,就像多边形一样。
我已经制作了自己的构造函数(在 Shape 和 Polygon 中),但它们什么都不做,只是空白范围以防止默认构造函数出现问题。

形状.h

#include "Point.h"

    class Shape  
    {  
    public:  
        Shape();  

        //Methods
        virtual double get_Perimeter() = 0; //Returns shape's perimeter
        virtual double get_Area() = 0; //Returns shape's area
        virtual void move(point p) = 0; //Moves the shape
    };

形状.cpp

#include "Shape.h"

shape :: Shape()
{
}

多边形.h

#include "Point.h"
#include "Shape.h"
#include <vector>
using namespace std;

class Polygon :public Shape
{
protected:
    //Fields
    vector<point> points; //Vector of the polygon's points

public:
    //Constructors
    Polygon();


    //Methods
    virtual int getNumOfPoints() = 0; //Returns number of points
    virtual vector<double> get_Sides() = 0; //Returns vector of side's length
    virtual vector<point> get_Points() = 0; //Returns vector of points

    virtual double get_Perimeter() = 0; //Returns shape's perimeter
    virtual double get_Area() = 0; //Returns shape's area
    virtual void move(point p) = 0; //Moves the shape
};

多边形.h

#include "polygon.h"
polygon :: polygon()
{
}

点.h

class point
{
protected:
    //Fields
    double x; //the X value of point
    double y; //the Y value of point

public:
    //Constructors
    point(double x, double y); //Creates new point with given params
    point(const point &other); //Creates new point with other point's params

    //Methods
    double get_X() const; //Returns X field
    double get_Y() const; //Returns Y field
    void move(int dx, int dy); //Adds given params to current params
    void move(point p); //Adds p's params to current params
};

主文件

vector<shape*> shapes;

    //Creating new tirangle
    point* p1 = new point(1,1);
    point* p2 = new point(5,1);
    point* p3 = new point(3,4);
    shapes.push_back(new triangle(*p1,*p2,*p3));

三角形.h

#include "point.h"
#include "polygon.h"
#include <vector>
using namespace std;

class triangle :public polygon
{

public:
    //Constructors
    triangle(point p1, point p2, point p3); //Creates new triangle with given params
    triangle(const triangle &other); //Copies other's params to new triangle

    //Methods
    point get_P1() const; //Returns p1
    point get_P2() const; //Returns p2
    point get_P3() const; //Returns p3

    int getNumOfPoints(); //Returns number of points
    vector<double> get_Sides(); //Returns vector of side's length
    vector<point> get_Points(); //Returns vector of points

    double get_Perimeter(); //Returns shape's perimeter
    double get_Area(); //Returns shape's area
    void move(point p); //Moves the shape
};

三角形.cpp

#include "triangle.h"
#include <cmath>

    //Constructors
    triangle :: triangle(point p1, point p2, point p3)
    {
        points[0] = p1;
        points[1] = p2;
        points[2] = p3;

    }
    triangle :: triangle(const triangle &other)
    {
        points[0] = other.get_P1();
        points[1] = other.get_P2();
        points[2] = other.get_P3();
    }

    //Methods
    point triangle :: get_P1() const
    {
        return points[0];
    }
    point triangle :: get_P2() const
    {
        return points[1];
    }
    point triangle :: get_P3() const
    {
        return points[2];
    }

    int triangle :: getNumOfPoints()
    {
        return points.size();
    }
    vector<double> triangle :: get_Sides()
    {
        vector<double> sides;
        sides[0] = sqrt(pow(points[0].get_X()-points[1].get_X(),2)+pow(points[0].get_Y()-points[1].get_Y(),2));
        sides[1] = sqrt(pow(points[1].get_X()-points[2].get_X(),2)+pow(points[1].get_Y()-points[2].get_Y(),2));
        sides[2] = sqrt(pow(points[2].get_X()-points[0].get_X(),2)+pow(points[2].get_Y()-points[0].get_Y(),2));
        return sides;
    }
    vector<point> triangle :: get_Points()
    {
        return points;
    }

    double triangle :: get_Perimeter()
    {
        vector<double> sides = this->get_Sides();
        return sides[0]+sides[1]+sides[2];
    }
    double triangle ::get_Area() //By Heron's Formula
    {
        vector<double> sides = this->get_Sides();
        double area = this->get_Perimeter()/2;
        double tmp = area;
        for(int i=0;i<3;i++)
            area*=(tmp-sides[i]);
        return sqrt(area);

    }
    void triangle :: move(point p)
    {
        for(int i=0;i<3;i++)
        {
            points[i].move(p);
        }
    }
4

3 回答 3

3

向量只能存储相同类型的对象。你需要的是一个拥有指针的容器:

#include <vector>
#include <memory>

std::vector<std::unique_ptr<Shape>> shapes;

shapes.emplace_back(new Triangle);
shapes.emplace_back(new Circle);
于 2012-11-15T16:40:14.610 回答
0

基本上是Kerres所说的。只是在类似情况下可能相关的附加说明。

如果您的Shape课程不是抽象的并且您使用

std::vector<Shape> shapes;

那么这个

shapes[i] = Circle(***);

将是有效的,但你的对象会被切片!这意味着,进一步派生类中的所有信息都将丢失。这是一个重要的事实要记住!

于 2012-11-15T16:47:26.423 回答
0

问题是构造函数triangle

triangle :: triangle(point p1, point p2, point p3)
{
    points[0] = p1;
    points[1] = p2;
    points[2] = p3;
}

这会尝试写入空向量的元素;[]如果向量足够大,您只能用于访问向量元素。您应该使用push_back()来增长向量:

points.push_back(p1);
points.push_back(p2);
points.push_back(p3);

或者,如果您可以使用 C++11,则从初始化列表中分配:

points = {p1, p2, p3};

复制构造函数也是如此;尽管根本没有必要声明复制构造函数,因为隐式生成的构造函数将正确复制点向量。

稍微偏离主题,如果您使用原始指针容器来管理对象生命周期,请非常小心:在不删除对象的情况下意外删除指针非常容易,从而导致内存泄漏。我建议使用智能指针。

还有一个潜在的问题:Shape没有虚拟析构函数,这意味着你不能删除向量中的任何形状;这样做可能会导致崩溃。你应该:

virtual ~Shape() {}
于 2012-11-15T17:00:30.740 回答