gpt4 book ai didi

c++传递指针

转载 作者:行者123 更新时间:2023-11-30 01:50:42 24 4
gpt4 key购买 nike

在掌握指针方面仍然有很多问题,我试着不使用它们,但我不确定如何最好地完成以下内容。

我有两个类 PointRay,我也确信在某处可能已经有一个点类的实现,我应该改用它。所以我有这两个类,我正在尝试创建一个射线,并最终将其传递给其他东西,但我收到一个错误:Thread: EXC_BAD_ACCESS (code=1, address=0x0 )` 当我按照我正在做的方式创建光线并尝试访问它时。

class Point {
public:
float x;
float y;
Point();
Point(float x, float y);
~Point();
};

Point::Point() {
x = 0;
y = 0;
}

Point::Point(float xPos, float yPos) {
x = xPos;
y = yPos;
std::cout << "point created at: " << x << ", " << y << std::endl;
}

Point::~Point() {
std::cout << "point destroyed" << std::endl;
}

class Ray {
public:

std::unique_ptr<Point> origin;

float angleInRadians;
float length;

float slope();
Point* pointAtDistance(float percentage);
Point* pointAtYPos(float yPos);

Ray(Point& origin, float angleInRadians, float length);
~Ray();
};

Ray::Ray(Point& origin, float angleInRadians, float length) {
origin = std::move(origin);
angleInRadians = angleInRadians;
length = length;
}

float Ray::slope() {
return tanf(angleInRadians);
}

Point* Ray::pointAtDistance(float percentage) {
return new Point(origin->x + (length * percentage) * cosf(angleInRadians), origin->y + (length * percentage) * sinf(angleInRadians));
}

Point* Ray::pointAtYPos(float yPos) {
float B = origin->y - (slope() * origin->x);
return new Point((yPos - B) / slope(), yPos);
}

Ray::~Ray() {
std::cout << "point released" << std::endl;
origin.release();
std::cout << "ray destroyed" << std::endl;
}

第三行是出错的地方。

std::unique_ptr<Point> point(new Point(x,y));
std::unique_ptr<Ray> ray(new Ray(*point, M_PI/4, 100));
std::cout << "RAY origin: " << ray->origin->x << ", " << ray->origin->y << std::endl;

最佳答案

我相信这应该可行——我只是从字面上从您的代码中删除了所有指针并使用了实例。以您的 C++ 知识水平,指针几乎总是不必要的。

class Point {
public:
Point();
Point(float x, float y);
~Point();

float x;
float y;
};

Point::Point() {
x = 0;
y = 0;
}

Point::Point(float xPos, float yPos) : x(xPos), y(yPos) {
std::cout << "point created at: " << x << ", " << y << std::endl;
}

Point::~Point() {
std::cout << "point destroyed" << std::endl;
}

class Ray {
public:
Ray(const Point& origin, float angleInRadians, float length);
~Ray();

float slope();
Point pointAtDistance(float percentage);
Point pointAtYPos(float yPos);

Point origin;

float angleInRadians;
float length;
};

Ray::Ray(const Point& o, float a, float l) : origin(o), angleInRadians(a), length(l) {
}

float Ray::slope() {
return tanf(angleInRadians);
}

Point Ray::pointAtDistance(float percentage) {
return Point(origin.x + (length * percentage) * cosf(angleInRadians), origin.y + (length * percentage) * sinf(angleInRadians));
}

Point Ray::pointAtYPos(float yPos) {
float B = origin.y - (slope() * origin.x);
return Point((yPos - B) / slope(), yPos);
}

Ray::~Ray() {
std::cout << "ray destroyed" << std::endl;
}

希望这能让您入门。祝你完成剩下的作业!

关于c++传递指针,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27135565/

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