如何在结构体内部实现指针
How to implement pointers inside a struct?
我是全新的c++(和编程),我试图实现一个"点"结构与以下访问器函数:
void setData(struct Point*, int u, int v);
void getRadius(struct Point*);
程序应该:
- 创建点P1在X=3, Y=4
- 输出P1的半径
使用这些指南:
struct Point A; // instantiates a point at A based on the struct Point
A_ptr = &A;
setData(A_ptr, 3, 4); // initializes the point A(3,4)
int Radius = getRadius(A_ptr); // calculates R and returns the value to Radius
下面是我的代码(显然是错误的):
#include "stdafx.h"
#include <iostream>
#include <cmath>;
using namespace std;
// the equation of a circle is:
// (x-a)^2 + (y-b)^2 = r^2, where (a,b) are the coordinates of the center
struct Point
{
int X; // x position
int Y; // y position
float R; // radius of circle
void setData(struct Point*, int u, int v);
float getRadius(struct Point*);
void inputCoordinates(int X, int Y);
};
int main()
{
struct Point A; // instantiates a point at A based on the struct Point
A_ptr = &A;
setData(A_ptr, 3, 4); // initializes the point A(3,4)
int Radius = getRadius(A_ptr); // calculates R and returns the value to Radius
cout << "The radius of a circle with points x = " << X << " and Y = " << Y << " is: " << Radius;
}
return 0;
}
void Point::setData(struct Point*, int u, int v)
{
X = u;
Y = v;
}
float Point::getRadius(struct Point*)
{
return sqrt(pow(X,2) + pow(Y,2));
}
问题本身很简单。我似乎无法将问题(和问题参数)"映射"到指针和结构的概念(我已经读到过,但对我来说仍然是模糊的)。
任何建议和指导都是非常感谢的。
提前谢谢你看一看。——瑞安
这是我在c++风格中的做法
#include <iostream>
#include <cmath>
using namespace std;
// the equation of a circle is:
// (x-a)^2 + (y-b)^2 = r^2, where (a,b) are the coordinates of the center
struct Point
{
int X; // x position
int Y; // y position
void setData(int u, int v);
float getRadius();
void inputCoordinates(int X, int Y);
};
int main()
{
Point A; // instantiates a point at A based on the struct Point
A.setData(3, 4); // initializes the point A(3,4)
float Radius = A.getRadius(); // calculates R and returns the value to Radius
cout << "The radius of a circle with points x = " << A.X << " and Y = " << A.Y << " is: " << Radius;
return 0;
}
void Point::setData(int u, int v)
{
X = u;
Y = v;
}
float Point::getRadius()
{
float value;
value = (float)sqrt(pow(X, 2) + pow(Y, 2));
return value;
}
在最后两个函数setData
和getRadius
中,您从不使用Point
参数。
改为:
void setData(Point* p, int u, int v)
{
p->X = u;
p->Y = v;
}
和
float getRadius(Point* p)
{
return sqrt(pow(p->X,2) + pow(p->Y,2));
}
也就是说,不要让它们成为Point
的成员函数。
你的观点::setData需要读取:
void Point::setData(struct Point*, int u, int v)
{
point->X = u;
point->Y = v;
}
,因为->是指针的成员访问器(相当于。对于非指针变量)
或者如果你在用C语言编程,让它成为一个函数,而不是一个方法:
void setData(struct Point*, int u, int v)
{
point->X = u;
point->Y = v;
}
我强烈建议您使用float或double作为X和Y坐标的数据类型。
你的半径不应该是结构体的成员。它在概念上不是点的一部分,而是描述两点之间的距离。
为getRadius()参数指定一个名称,例如"p"。那么getRadius()方法的主体可以简单地为
{
float dx = X - p->x;
float dy = Y - p->y;
return sqrt(dx * dx + dy * dy); // Pythagorean formula
}
我希望这能给你正确的方向。
相关文章:
- 为什么我们要为avl树实现返回一个指向节点的指针,而不是void函数
- Qt中的实现指针(PIMPL)
- 如何实现容纳整数和无效指针的双向链表?
- 给定一个指向堆分配内存的指针,智能指针实现如何为其找到合适的释放函数?
- 使用共享指针实现复制 c'tor?
- 使用指针在分区函数中实现随机分区点
- 涉及指针和手动实现的矩阵类的问题
- 将C++子类成员函数(虚拟实现)传递给 C 类型函数指针
- 使用智能指针指向 C 库中的结构,该结构通过 typedef 隐藏实现(即不完整的类型)
- 未分配被释放的指针(将堆栈实现为链表时)
- 如何正确实现具有原始指针的类的复制构造函数?
- 如何在C++中重新实现包含指针的 STL 容器的类的迭代器
- 我们可以在不使用head指针的情况下通过使用head的简单变量而不是head的指针来实现链表吗
- 如何在CRTP实现中传递基类指针
- 如何在 c++ 中实现指针的类型
- 实现指针队列
- 实现指针元组的深度复制
- 如何实现指针的<<和>>?
- 如何在结构体内部实现指针
- 实现指针转换操作符