如何访问C++链表中的对象数据
How do I access object data in a C++ linked list?
我将MovingSphere对象存储在一个链表中。MovingSphere继承Sphere。通过读取由空白分隔的整数组成的文本文件来初始化球体。
当我访问球体的半径时,它会返回正确的数字,但当我试图获得X、Y、Z坐标时,它返回NAN。我试着通过NetBeans调试器运行它,但没能收集到太多有用的信息。我知道存储了正确的值,只是不知道为什么我不能正确访问这些坐标。
我仍然在为从Java到C++的转换而挣扎,所以请原谅我,这是一个明显的问题。
Main
#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 */
移动球体.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);
}
Sphere.h
#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;
}
点.h
#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();
}
相关文章:
- 为什么不能修改对象中的值?另外,我如何改进此链表?
- C++ 创建包含链表和字符串的对象的链接列表时出错
- 链表包含 c++ 中不同的派生类对象
- 如何深度复制链表对象指针
- 如何通过不同的指针使用类的对象访问结构?(链表)(C++)
- 节点链表中的对象
- 按引用或值传递链表对象更好吗?
- 基于范围的 for 循环,用于包含C++中的指针的自定义链表,仅返回对象
- C++ 如何创建链表的副本作为类对象
- 无法分配给链表中的对象
- C++ 链表:"->"运算符无法更改节点对象的内部值
- 具有自己的链表的对象
- 存储类对象的C++模板链表-未处理的异常访问冲突读取位置
- 从链表访问对象(自定义实现)
- 创建面向对象的链表时出现编译问题(编译器错误C2664)
- 对象数组作为链表中的字符数组 - 我对对齐的假设是否有效
- C++ 链表:对象的索引
- 如何将链表对象写入文件
- 在c++中创建一个二维链表对象
- 调用在另一个对象内部定义的链表对象的函数