c ++中的双继承运行时错误

时间:2012-11-15 16:37:52

标签: c++ oop inheritance

运行包含以下类的项目时出现运行时错误:
Shape - 摘要
Polygon :public Shape - 摘要
Triangle :public Polygon - 正常班级

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

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

我遇到了运行时错误 它是否与双重继承有关,还是我的代码中存在问题?
因为我也去了课Circle :public Shape并且在做

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

一切正常..

修改
正如大多数人猜测的那样vector<Shape*> shapes;。  我不会走出医生的边界 我已将其更改为.push_back(new Circle()),但它仍然只是崩溃了。

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

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

Shape.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
    };

Shape.cpp

#include "Shape.h"

shape :: Shape()
{
}

Polygon.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
};

Polygon.h

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

Point.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
};

的main.cpp

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));

Triangle.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
};

Triangle.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);
        }
    }

3 个答案:

答案 0 :(得分:3)

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

#include <vector>
#include <memory>

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

shapes.emplace_back(new Triangle);
shapes.emplace_back(new Circle);

答案 1 :(得分:0)

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

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

std::vector<Shape> shapes;

然后这个

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

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

答案 2 :(得分: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() {}