C++试图根据提供的坐标创建一个点
C++ trying to create a point from the provided coordinates
为此,我应该能够从指针(双*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);
}
相关文章:
- 如何创建一个CMake变量,除非显式重写,否则使用默认值
- 创建一个函数以在输入为负数或零时输出字符串.第一次执行用户定义的函数
- 试图在visual studio上用C++创建一个桌面应用程序
- 如何创建一个空的全局类并在启动时实例化它
- 有没有一种方法可以创建一个带有哈希表的数据库,该哈希表具有恒定时间查找功能
- 如何创建一个QTableWidgetItem,用长文本右对齐,左边有省略号
- 我正在尝试使用 c++ 创建一个货币转换程序,我不知道如何继续
- visual是否可以在c++中创建一个接收无限数量相同类型(或至少相当数量)参数的函数
- 试图创建一个多线程程序来查找0-100000000之间的总素数
- 如何创建一个函数来计算并返回平均值、最大值和最小值
- 如何使用CLion在Mac上创建一个新的.txt文件
- 如何在没有数据拷贝的情况下从指针创建一个Eigen VectorXd对象
- 在c++中为double类型的数组创建一个unique_ptr
- 我可以创建一个包含两个变量的 for 循环,但时间复杂度仍然为 O(n) 吗?
- GLEW/GLUT:调用init并创建一个窗口后,取消初始化并重新初始化?
- 我想通过带有C++和Python的插件创建一个可扩展的应用程序
- 在 c++ 中,有一种方法可以创建一个包含地图作为值的树状地图?
- 模板化接口 - 创建一个泛型模板类以返回任何容器
- 创建一个包含 c++ 默认值的环境文件
- 如何创建一个类,以便向量工作 std::vector<MyClass<int>> v{ 1,2,3 };