简单的计算返回错误的值

Simple calculations return wrong Value

本文关键字:错误 计算 简单 返回      更新时间:2023-10-16

我写了一个程序,要求用户输入三个值,然后从中计算出其他值。

但是它返回垃圾。例如:

Track.h

class Track : public World
{
    public:
        friend class Vehicle;
        Track();
        virtual ~Track();
        float GetLenght() {return m_Lenght;}
        float GetSlope() {return m_Slope;}
        void SetSlope(float value) {m_Slope = value;}
        float GetAcceleration();
    protected:
    private:
        const float m_Lenght = 2; //m
        float m_Slope;
        float m_TrackAcceleration;
};

Track.cpp

#include "Track.h"
#include "World.h"
#include <math.h>
#include "Vehicle.h"
#define PI 3.14159265359
[...]
float Track::GetAcceleration() {
    World *Earth = new World();
    Vehicle *Car = new Vehicle();
    m_TrackAcceleration = Earth->Accelerate(Car->GetWeight())*sin(1*PI/180);
    return m_TrackAcceleration;
}

main.cpp

int main()
{
    World *Earth = new World();
    Track *Track1 = new Track();
    Vehicle *Car = new Vehicle();
    Mass *Mass1 = new Mass();
    float MassWeight, VehicleWeight, Slope;
    cout << "Mass Weight:"; cin >> MassWeight;
    cout << "Vehicle Weight:"; cin >> VehicleWeight;
    cout << "Slope:"; cin >> Slope;
    Mass1->SetWeight(MassWeight);
    Car->SetWeight(VehicleWeight);
    Track1->SetSlope(Slope);
    cout << "Acceleration Force:" << Track1->GetAcceleration() << endl << endl;
};

Vehicle.h

class Vehicle : public World
{
    public:
        friend class Track;
        Vehicle();
        virtual ~Vehicle();
        float GetWeight() {return m_VehicleWeight;}
        void SetWeight(float value) {m_VehicleWeight = value;}
        float GetSpeed(float seconds);
        float GetAcceleration();
        float GetDistance(float seconds);
    protected:
    private:
        float m_VehicleWeight;
        float m_Speed;
        float m_Distance;
        float m_VehicleAcceleration;
};

World.h

class World
{
    public:
        World();
        virtual ~World();
        float GetGravity (){return m_Gravity;}
        float Accelerate (float mass);
    protected:
    private:
        const float m_Gravity = 9.81; // m/s^2
        float m_WorldAcceleration;
};

应该返回0,171208....,但是它返回的是1.91825e-039以及Mass Weight=1Vehicle Weight=1Slope=1

对此有什么想法吗?

我认为问题在于您不知道在局部作用域中创建对象时会发生什么。由于某种原因,您的代码创建了两组独立的Earth和Car对象。

float Track::GetAcceleration() 
{
    World *Earth = new World();   // this is a brand new World object
    Vehicle *Car = new Vehicle();  // this is a brand new Vehicle object
    // now you're calcuating acceleration with these brand new objects 
    m_TrackAcceleration = Earth->Accelerate(Car->GetWeight())*sin(1*PI/180);
    return m_TrackAcceleration;
}

你不仅有内存泄漏,你正在创建新对象,我们不知道新构造的对象有什么值,因为你没有发布WorldVehicle的构造函数的代码。

然后在main()中这样做:

int main()
{
   World *Earth = new World();  // a brand new object
   Track *Track1 = new Track();  // another brand new object
   Vehicle *Car = new Vehicle();  // yet another 
   Mass *Mass1 = new Mass();  // and another
   float MassWeight, VehicleWeight, Slope;
   cout << "Mass Weight:"; cin >> MassWeight;
   cout << "Vehicle Weight:"; cin >> VehicleWeight;
   cout << "Slope:"; cin >> Slope;
   Mass1->SetWeight(MassWeight);
   Car->SetWeight(VehicleWeight);
   Track1->SetSlope(Slope);
   // This call knows nothing about the Earth or Car objects you created in main().
   cout << "Acceleration Force:" << Track1->GetAcceleration() << endl << endl;
}

好的,你将值赋给在main()中声明的对象,但是当你调用GetAcceleration时,该函数创建了两个与main中创建的对象无关的全新对象。你在main()中创建的对象的引用在哪里?

首先,您需要认识到c++不是Java。你不需要new来创建对象。

int main()
{
   World Earth;  // a brand new object
   Track Track1;  // another brand new object
   Vehicle Car;  // yet another 
   Mass Mass1;  // and another
   float MassWeight, VehicleWeight, Slope;
   cout << "Mass Weight:"; cin >> MassWeight;
   cout << "Vehicle Weight:"; cin >> VehicleWeight;
   cout << "Slope:"; cin >> Slope;
   Mass1.SetWeight(MassWeight);
   Car.SetWeight(VehicleWeight);
   Track1.SetSlope(Slope);
   // This call knows nothing about the Earth or Car objects you created in main().
   cout << "Acceleration Force:" << Track1.GetAcceleration() << endl << endl;
}

这段代码现在没有内存泄漏了。

现在,在GetAcceleration中,它如何知道你在main()中创建的对象,除非你将它们作为参数传递?

  float Track::GetAcceleration(World& theWorld, Vehicle& theVehicle) 
  {
    m_TrackAcceleration = theWorld.Accelerate(theVehicle.GetWeight())*sin(1.0*PI/180);
    return m_TrackAcceleration;
  }

来自main()的调用看起来像这样:

   cout << "Acceleration Force:" << Track1.GetAcceleration(Earth, Car) << endl << endl;

您的Car车辆刚刚创建。假设它没有将成员初始化为不同于0的值,那么在乘法之后应该得到一个0的值。

1.91825e-039是一个接近于零的值。这种差异可能是由于你所做的斜率,加速度等代数运算的结果。

至少你的问题没有说明为什么我们应该期望一个不同的值