gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-28 00:52:24 25 4
gpt4 key购买 nike

我在运行包含以下类的项目时遇到运行时错误:
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()
{
}

多边形.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
};

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

三角形.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);
}
}

最佳答案

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

#include <vector>
#include <memory>

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

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

关于c++ - C++中的双重继承运行时错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13402121/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com