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

C++ trying to create a point from the provided coordinates

本文关键字:创建 一个 坐标 C++      更新时间:2023-10-16

为此,我应该能够从指针(双*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);

但是我接收到CCD_ 1错误。我想知道如何开始这项工作,以确保点阵列中的所有元素都具有相同数量的维度,并找到x和y的最小值和最大值。

通过提供额外的头,我可以猜测分段错误的原因是访问了未初始化的内存。

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

并且不要忘记在CCD_ 7的析构函数中对CCD_。

~Point() {
delete coordinates_;
}

或者最好使用动态数组,如std::vector<double>

此外,这个循环没有任何用处。考虑删除它。

for (size_t i = 0; i < dimensions_; ++i) {
*(coordinates_ + i);
}
相关文章: