我将MovingSphere对象存储在链接列表中。 MovingSphere继承了Sphere.Sphere通过从文本文件中读取来初始化,这些文本文件由空格隔开。
当我访问球体的半径时,它将返回正确的数字,但是当我尝试获取X,Y,Z坐标时,它将返回NAN。我尝试通过NetBeans调试器运行它,但无法收集很多有用的信息。我知道要存储正确的值,我只是不知道为什么我不能正确访问这些坐标。
我仍在努力从Java转换为C ++,因此请原谅我这是一个显而易见的问题。
主要
#include <iostream>
#include <fstream>
#include <list>
#include "MovingSphere.h"
using namespace std;
typedef list<MovingSphere>::iterator iter;
static double INCREMENT = .01; // time increment
static int CUBE_SIZE = 1000; // distance across any given cube wall
int main() {
ifstream inFile("sphere.txt");
double X, Y, Z;
int R, DX, DY, DZ;
list<MovingSphere> sphereList;
MovingSphere msInstance;
iter it = sphereList.begin();
// populate SphereList
if (inFile) { // If file is not empty.
while (inFile >> X >> Y >> Z >> R >> DX >> DY >> DZ) {
// create sphere
msInstance = MovingSphere(X, Y, Z, R, DX, DY, DZ);
cout << endl <<
"X = " << msInstance.getCenterX() << endl <<
"Y = " << msInstance.getCenterY() << endl <<
"Z = " << msInstance.getCenterZ() << endl <<
"R = " << msInstance.getRadius() << endl <<
"DX = " << msInstance.getDX() << endl <<
"DY = " << msInstance.getDY() << endl <<
"DZ = " << msInstance.getDZ() << endl;
sphereList.insert(it, msInstance);
++it;
} // end while
} // end if
return 0;
}
MovingSphere.h
#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 */
MovingSphere.cpp
#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 */
Sphere.cpp
#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 */
点.cpp
#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...
最佳答案
将返回值添加到getCenter:
double Sphere::getCenterX() const {
return center.getX();
}