简单的计算返回错误的值
Simple calculations return wrong Value
我写了一个程序,要求用户输入三个值,然后从中计算出其他值。
但是它返回垃圾。例如:
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=1
、Vehicle Weight=1
和Slope=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;
}
你不仅有内存泄漏,你正在创建新对象,我们不知道新构造的对象有什么值,因为你没有发布World
或Vehicle
的构造函数的代码。
然后在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
是一个接近于零的值。这种差异可能是由于你所做的斜率,加速度等代数运算的结果。
至少你的问题没有说明为什么我们应该期望一个不同的值
相关文章:
- (C++)分析树以计算返回错误值的简单算术表达式
- 我的字符计数代码计算错误.为什么
- OpenCV - Python 断言错误:SAD 算法 - 立体相机视差图计算
- 如何在常量计算表达式中获取编译时错误?
- 计算阶乘的 C++17 倍表达式中的错误
- 为什么 2 个双精度值之间的差值计算错误?
- 我在c++中遇到了一个奇怪的错误,其中一个计算2个小整数加法的语句溢出到一个长值中
- 错误 C2064:术语的计算结果不是采用 3 个参数的函数
- 错误:一元"*"的类型参数无效(具有"int"):使用 mergesort 计算
- C++ openmp 并行计算计算错误的结果
- 为什么按位运算符在使用与整数中提供的位数相同的位数计算可能的最大范围时会产生错误
- 术语不计算为函数采用 1 个参数错误?
- Travis CI 中奇怪的编译器错误不会在本地计算机上发生
- 点云库:计算筛选关键点 - 输入云错误
- 在线程错误 C2064 中:term 的计算结果不为 0 个参数的函数
- GetAsyncKeyState(VK_RETURN) 错误地计算为 true
- 错误 C2064:term 的计算结果不是采用 1 个参数的函数 - 关于线程的一些东西
- log2 计算在 CPP 中给出了错误的结果
- 错误计算RSI或错误在MetaTrader 4
- c++程序错误计算