我写了一个程序,询问用户三个值,然后计算其他值。
但不知何故,它会返回垃圾。 例如:
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
。
对此有何看法?
答案 0 :(得分:2)
我认为问题在于您不了解在本地范围内创建对象时会发生什么。出于某种原因,您的代码会创建两组独立的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
或{{1的构造函数的代码}}。
然后在main()中执行此操作:
Vehicle
好的,您将值分配给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()
时,该函数会创建两个与{{1}中创建的对象无关的全新对象}}。您对main()中创建的对象的引用在哪里?
首先,您需要意识到C ++不是Java。您不需要GetAcceleration
来创建对象。
main
此代码中现在没有内存泄漏。
现在,在GetAcceleration中,除非将它们作为参数传递,否则如何知道在main()中创建的对象?
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;
}
的来电将如下所示:
float Track::GetAcceleration(World& theWorld, Vehicle& theVehicle)
{
m_TrackAcceleration = theWorld.Accelerate(theVehicle.GetWeight())*sin(1.0*PI/180);
return m_TrackAcceleration;
}
答案 1 :(得分:0)
您的Car
车辆刚刚创建。假设它没有将成员初始化为不同于零的值,则在乘法后应该得到零值。
1.91825e-039
是接近零的值。差异可能是您使用斜坡地球加速度等进行代数的结果。
至少你的问题没有说明为什么人们应该期待不同的价值。