简体   繁体   English

如何访问C ++链表中的对象数据?

[英]How do I access object data in a C++ linked list?

I'm storing MovingSphere objects in a linked list. 我将MovingSphere对象存储在链接列表中。 MovingSphere inherits Sphere.The spheres are initialized by reading from a text file of integers separated by white space. MovingSphere继承了Sphere.Sphere通过从以空格分隔的整数文本文件中读取来初始化球体。

When I access the radius of the sphere, that returns the proper number, but when I try to get X, Y, Z coordinates, it returns NAN. 当我访问球体的半径时,它将返回正确的数字,但是当我尝试获取X,Y,Z坐标时,它将返回NAN。 I tried running it through the NetBeans debugger, but I wasn't able to gather much helpful information. 我尝试通过NetBeans调试器运行它,但无法收集很多有用的信息。 I know the proper values are being stored, I just don't know why I can't properly access these coordinates. 我知道要存储正确的值,我只是不知道为什么我不能正确访问这些坐标。

I'm still struggling with the swap from Java to C++ so forgive me of this is a blatantly obvious problem. 我仍在为从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 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 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);
}

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 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;
}

Point.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 */

Point.cpp 点.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...

Output 输出量

X = nan
Y = nan
Z = nan
R = 3
DX = -10
DY = 5
DZ = 0
etc...

Add return to getCenter: 将返回值添加到getCenter:

double Sphere::getCenterX() const {
    return center.getX();
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM