C++ 如何访问C++;链表?
我正在链接列表中存储MovingSphere对象。MovingSphere继承Sphere。通过读取由空格分隔的整数组成的文本文件来初始化球体 当我访问球体的半径时,它返回正确的数字,但当我尝试获取X,Y,Z坐标时,它返回NAN。我尝试通过NetBeans调试器运行它,但无法收集到很多有用的信息。我知道存储了正确的值,我只是不知道为什么我不能正确访问这些坐标 <>我仍然在努力从java到C++的交换,所以请原谅我这是一个明显的问题。 主要 MovingSphere.cpp Sphere.h Sphere.cpp Point.cpp 输出C++ 如何访问C++;链表?,c++,linked-list,iteration,C++,Linked List,Iteration,我正在链接列表中存储MovingSphere对象。MovingSphere继承Sphere。通过读取由空格分隔的整数组成的文本文件来初始化球体 当我访问球体的半径时,它返回正确的数字,但当我尝试获取X,Y,Z坐标时,它返回NAN。我尝试通过NetBeans调试器运行它,但无法收集到很多有用的信息。我知道存储了正确的值,我只是不知道为什么我不能正确访问这些坐标 我仍然在努力从java到C++的交换,所以请原谅我这是一个明显的问题。 主要 MovingSphere.cpp Sphere.h Sphe
将返回添加到getCenter:
double Sphere::getCenterX() const {
return center.getX();
}
建议:将
Sphere
的析构函数设置为虚拟的。您确实需要将问题缩小到一小段仍然编译并演示问题的代码。@MarkGarcia我在以前的迭代中看到过(也做过)这一点,但我真的不理解其重要性。垃圾收集对我来说仍然是个谜。@JerryCoffin谢谢。我会这么做。@Justin当您动态分配MovingSphere
(使用new
)并delete
从基类类型指针(Sphere*
)中删除它时,这一点很重要。如果将Sphere
的析构函数设置为虚拟,则会调用相应的析构函数(即MovingSphere
)
#ifndef MOVINGSPHERE_H
#define MOVINGSPHERE_H
#include "Sphere.h"
class MovingSphere : public Sphere {
public:
MovingSphere(); // default constructor
MovingSphere(double X, double Y, double Z, int R); // Sphere with zero velocity
MovingSphere(double X, double Y, double Z, int R,
int DX, int DY, int DZ); // moving sphere constructor
void MovingSphere::updateCenter(double multiplier); // declaration
private:
// delta X, etc. define rate of change ie. (+/-) units/second
int dx, dy, dz; // sphere vector
};
#endif /* MOVINGSPHERE_H */
#include "MovingSphere.h"
MovingSphere::MovingSphere() : Sphere(), dx(0), dy(0), dz(0) {
} // default constructor (point at 0,0,0, with zero velocity)
MovingSphere::MovingSphere(double X, double Y, double Z, int R) :
Sphere(X, Y, Z, R), dx(0), dy(0), dz(0){
} // sphere with zero velocity
MovingSphere::MovingSphere(double X, double Y, double Z, int R, int DX, int DY, int DZ) :
Sphere(X, Y, Z, R), dx(DX), dy(DY), dz(DZ){
} // moving sphere constructor
// update location of sphere (mutator)
void MovingSphere::updateCenter(double multiplier) {
adjustCenter(dx * multiplier, dy * multiplier, dz * multiplier);
}
#ifndef SPHERE_H
#define SPHERE_H
#include "Point.h"
class Sphere {
public:
Sphere(); // default constructor (a point at 0,0,0)
Sphere (double X, double Y, double Z, int R); // sphere constructor
void setRadius(int newRadius); // declaration
void adjustCenter(double DX, double DY, double DZ); // declaration
int getRadius() const; // declaration
double getCenterX() const; // declaration
etc...
private:
Point center; // center of sphere
int radius; // radius of sphere
};
#endif /* SPHERE_H */
#define _USE_MATH_DEFINES
#include <math.h>
#include "Sphere.h"
Sphere::Sphere() :
center(), radius(0) {// default constructor (a point at 0,0,0)
}
Sphere::Sphere(double X, double Y, double Z, int R) : center(X,Y,Z), radius(R) {
} // end sphere constructor
// set the sphere's radius (mutator)
void Sphere::setRadius(int newRadius) {
radius = newRadius;
}
// set the sphere's center (mutator)
void Sphere::adjustCenter(double DX, double DY, double DZ) {
center.setX(center.getX() + DX);
center.setY(center.getX() + DY);
center.setZ(center.getZ() + DZ);
}
// get the sphere's center X coordinate (constant function)
double Sphere::getCenterX() const {
center.getX();
}
etc...
// get the sphere's radius (constant function)
int Sphere::getRadius() const {
return radius;
}
#ifndef POINT_H
#define POINT_H
class Point {
public:
Point (); // default constructor (0,0,0);
Point(double X, double Y); // constructor for 2d point
Point(double X, double Y, double Z); // constructor for 3d point
void setX(double newX); // declaration
double getX() const; // declaration
etc...
private:
double x, y, z; // coordinates of point
};
#endif /* POINT_H */
#include "Point.h"
Point::Point() : x(0), y(0), z(0) { // default constructor (point at 0,0,0)
}
// constructor for 2d point
Point::Point(double X, double Y) : x(X), y(Y), z(0) {
}
// constructor for 3d point
Point::Point(double X, double Y, double Z) : x(X), y(Y), z(Z) {
}
// set the points X coordinate (mutator)
void Point::setX(double newX) {
x = newX;
// get the points X coordinate (constant function)
double Point::getX() const {
return x;
}
etc...
X = nan
Y = nan
Z = nan
R = 3
DX = -10
DY = 5
DZ = 0
etc...
double Sphere::getCenterX() const {
return center.getX();
}