我试图创建一个程序来为我的Kerbal Space Program游戏找出Delta-V,而C ++(在Eclipse IDE中运行)似乎相信我试图调用" log ()"函数实际上是我引用一个未创建的函数。我非常感谢你对此事的任何帮助!
#include <iostream>
#include <cmath>
using namespace std;
int main() {
cout << "Hello. Welcome to the Kerbal Space Program Delta V Calculator. \n";
cout << " \n";
cout << "Note that each stage must use the same engine for this calculator.";
cout << "\n";
cout << "\nHow many stages make up your rocket? :";
int stageNumber;
cin >> stageNumber;
//cout << "Your rocket has " << stageNumber << " stages.\n";
cout << "\n\nStart from the bottom stage, please. ";
//ACTUAL DELTA V CALCULATIONS
for(int currentStage = 1; currentStage <= stageNumber; currentStage = currentStage + 1){
cout << "What is the total mass of this stage? :";
int totalMass;
cin >> totalMass;
cout << "What is the fuel mass of this stage? :";
int fuelMass;
cin >> fuelMass;
cout << "\n";
int dryMass;
dryMass = totalMass - fuelMass;
cout << "Your dry mass is" << dryMass << "\n";
cout << "\n";
cout << "Give the specific impulse of this stage's engine. \n";
int iSP;
cin >> iSP;
cout << "Here is the Delta V of your rocket.\n";
int deltaMass;
deltaMass = totalMass/dryMass;
int deltaV;
deltaV = iSP * log(deltaMass);
cout << deltaV;
exit(0);
}
}
答案 0 :(得分:1)
使用数学函数时,您可能需要在数学库中进行链接。
通常在编译命令中添加-lm
选项。
答案 1 :(得分:0)
我认为您的计算要使用double
(我更喜欢这里)或float
变量。现在所有小数位都被截断,因为你进行整数除法。此外,您的循环不会执行多次迭代,因为调用exit(0);
会终止整个程序。删除它并使用return 0;
退出主要功能。由于log(int)
中没有cmath
,因此会出现您的错误消息。使用double
也可以解决这个问题。如果您收到链接器错误,请附加链接器选项-lm
。