English 中文(简体)
未从外部类别获得数据时显示的行业内数据变化
原标题:Changes to data inside class not being shown when accessed from outside class

我有两门班,即卡人和人。 驾驶员作为其成员之一,担任司机。 我想搬进一辆汽车,同时跟踪其位置,并将司机带进车内并到达车址。 然而,虽然这一工程是从内部进行的(我已印刷出计算值的数值),但当我试图从主人那里获取数据时,就没有任何东西。 I.e. 阵列的位置消失。 我很想知道,我设立这几类课程的方式是否错了——这是否是一个物体范围的问题?

我已做过简化,因此我只需要做。 希望>。 The Constructioner (") with non-zero.

class Car{

public: 

       Container(float=0,float=0,float=0);
       ~Container();
       void move(float);
       void getPosition(float[]);
       void getDriverPosition(float[]);

private:

       float position[3];
       Person driver;
       float heading;
       float velocity;

};


class Person{

public: 

       Person(float=0,float=0,float=0);
       ~Person();
       void setOffset(float=0,float=0,float=0);
       void setPosition(float=0,float=0,float=0);
       void getOffset(float[]);
       void getPosition(float[]);

private:

       float position[3];
       float offset[3];
};

一些职能:

void Car::move(float time){

    float distance = velocity*time;
    location[0] += distance*cos(PI/2 - heading);
    location[1] += distance*sin(PI/2 - heading);

    float driverLocation [3];
    float offset[3];
    driver.getOffset(offset);

    for (int i = 0; i < 3; i++){
       driverLocation[i] = offset[i] + location[i];
    }
    driver.setPosition(driverLocation[0],driverLocation[1],driverLocation[2]);
}

void Car::getDriverPosition(float p[]){

    driver.getPosition(p);
}

void Person::getPosition(float p[]){

    for (int i = 0; i < 3; i++){
       p[i] = position[i];
    }
}

void Person::getOffset(float o[]){

    for (int i = 0; i < 3; i++){
       o[i] = offset[i];
    }
}

主要:

Car * car = new Car();
car->move();
float p[3];
car->getDriverPosition(p);

当我印刷移动功能中的驾驶员时,我有实际的非零值。 我在主机中印刷第[]页,我得到的是零。

新发现的问题: 我每次都会发现有人叫走()个人班子,从而删除所有数据。 任何人都知道如何做到这一点? 我在任何地方都没有叫 des子,我如何防止它发生。

最佳回答

因此,我认为我是这样说的。 我认为,问题在于,我把目标按价值推移到移动(移动)功能中。 我转而把它当作物体的点子,我工作!

问题回答

您从未把驾驶员阵列通过到<条码>人——它应如何知道如何移走? (i) 您需要打电话driver->setPosition(driverLocation) withinmove

在法典中,你从未显示过任何人。 职位

您在称为“驾车”的移动中设定了当地变量。

以上法典的问题是行文。

driver->getOffset(offset);

The private member driver is not a pointer type. Change this to: driver.getOffset(offset)

另一种解决办法类似:

// header
class Car
{
  private:
    Person* driver;

  public:
    Car();
    ~Car();
}

// source
Car::Car()
{
  driver = new Person();
}

Car::~Car()
{
  delete driver;
}




相关问题
Undefined reference

I m getting this linker error. I know a way around it, but it s bugging me because another part of the project s linking fine and it s designed almost identically. First, I have namespace LCD. Then I ...

C++ Equivalent of Tidy

Is there an equivalent to tidy for HTML code for C++? I have searched on the internet, but I find nothing but C++ wrappers for tidy, etc... I think the keyword tidy is what has me hung up. I am ...

Template Classes in C++ ... a required skill set?

I m new to C++ and am wondering how much time I should invest in learning how to implement template classes. Are they widely used in industry, or is this something I should move through quickly?

Print possible strings created from a Number

Given a 10 digit Telephone Number, we have to print all possible strings created from that. The mapping of the numbers is the one as exactly on a phone s keypad. i.e. for 1,0-> No Letter for 2->...

typedef ing STL wstring

Why is it when i do the following i get errors when relating to with wchar_t? namespace Foo { typedef std::wstring String; } Now i declare all my strings as Foo::String through out the program, ...

C# Marshal / Pinvoke CBitmap?

I cannot figure out how to marshal a C++ CBitmap to a C# Bitmap or Image class. My import looks like this: [DllImport(@"test.dll", CharSet = CharSet.Unicode)] public static extern IntPtr ...

Window iconification status via Xlib

Is it possible to check with the means of pure X11/Xlib only whether the given window is iconified/minimized, and, if it is, how?

热门标签