运行包含以下类的项目时出现运行时错误:
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);
}
}