gpt4 book ai didi

C++ 试图从提供的坐标创建一个点

转载 作者:行者123 更新时间:2023-11-27 23:36:31 25 4
gpt4 key购买 nike

为此,我应该能够从指针 (double *coordinates_) 和它具有的维数 (size_t dimensions_) 构造一个点,就像 2d、3d、#d。我有一个名为

的构造函数声明

编辑:

#include <algorithm>
#include <cassert>
// using assert
#include <cmath>
// using fabs
#include <cstddef>
// using size_t


class Point {
/* Used for white-box testing.
*/
friend class PointInspector;

public:
/* Calculates a bounding box for the stored points and stores in the two
* output paramters.
* - PRECONDITION:
* - all elements in the points array have the same number of dimensions
* - lower_left and upper_right output params have same number of
* dimensions as an element of points array
* - PARAMETERS
* - points: array of Point elements around which a bounding box is
* calculated
* - lower_left: output parameter to store the lower left point of a
* bounding-box containing all the points in the array
* - upper_right: output parameter to store the upper right point of a
* bounding-box containing all the points in the array
* - POSTCONDITION: the lower_left and upper_right store the lower left and
* upper right points of a bounding box containing all elements of the
* input array points.
*/
static void CalcBoundingBox(
const Point* points,
size_t size,
Point* lower_left,
Point* upper_right);

/* Initialization Constructors: create a point from the provided coordinates
*/
Point();
Point(double x, double y);
Point(double x, double y, double z);
Point(const double coordinates[], size_t dimensions);
/* Copy Constructor: implements a deep copy */
Point(const Point& that);

/* Destructor: deletes any allocated memory
*/
~Point();


/* Assignment Operator: implements a deep copy
*/
Point &operator=(const Point& rhs);

/* Accessor/Mutator Operator: provides access to private member
* - PRECONDITIONS: parameter dimension is a valid axis of the point
* - RETURNS: reference to one of the coordinates of the point
*/
double &operator[](size_t dimension);

/* Equality Operator: defines points equal when the difference between
* axes is no more than kMax_point_diff.
* - PRECONDITIONS: points' dimensions are equal
*/
bool operator==(const Point& rhs) const;

private:
static double kMax_diff; // floating point equality

double *coordinates_; // coordinates of point
size_t dimensions_; // dimensions of point (2d, 3d, Nd)
};

这是我试图构建这一点的尝试。

void Point::CalcBoundingBox(const Point* points, size_t size, 
Point* lower_left, Point* upper_right) {


// find point with min x value and min y value

// put min x and y in the lower left output parameter

// find point with max x value and max y value

// put max x and y in the upper right output parameter
}

我的问题是有人可以帮助我从边界框开始。我知道我应该找到什么(x 和 y 的最小点/x 和 y 的最大点),但我不知道从哪里开始?

一开始我确实有这段代码:


dimensions_ = size;
coordinates_ = new size_t[dimensions_];
for (size_t i = 0; i < dimensions_; ++i)
*(coordinates_ + i) = *(points + i);

但我收到一个非静态成员引用必须相对于特定对象错误。我想知道我将如何开始这个以确保点数组中的所有元素具有相同的维数并找到 x 和 y 的最小值和最大值。

最佳答案

通过提供额外的 header ,我可能会猜测出现段错误的原因是访问未初始化的内存。

类(class)Point声明成员 coordinates_作为指针。我看不到任何代码来分配这个指针可能指向的数组。因此我假设这个构造函数:

Point::Point(double x, double y) {
coordinates_[0] = x;
coordinates_[1] = y;
for (size_t i = 0; i < dimensions_; ++i) {
*(coordinates_ + i);
}

访问由 coordinates_ 指向的内存尚未分配。因此 segmentation error

为了解决这个问题——你可以动态分配数组:

Point::Point(double x, double y) {
dimensions_ = 2;
coordinates_ = new double[dimensions_];
coordinates_[0] = x;
coordinates_[1] = y;
}

...别忘了 delete它在 Point 的析构函数中.

~Point() {
delete coordinates_;
}

或者更好 - 使用动态数组,例如 std::vector<double> .

此外 - 这个循环没有任何用处。考虑将其移除。

    for (size_t i = 0; i < dimensions_; ++i) {
*(coordinates_ + i);
}

关于C++ 试图从提供的坐标创建一个点,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58848952/

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