//convert sunType to sunTypePercentage
float LocationData::computeCivIndex(string st, int earth, int moons, float particle, float plasma)
{
float sunTypePercent;
if(st == "Type 0")
{
sunTypePercent = 80.0;
}
else if(st == "Type B")
{
sunTypePercent = 45.0;
}
else if(st == "Type A")
{
sunTypePercent = 60.0;
}
else if(st == "Type F")
{
sunTypePercent = 75.0;
}
else if(st == "Type G")
{
sunTypePercent = 90.0;
}
else if(st == "Type K")
{
sunTypePercent = 80.0;
}
else if(st == "Type M")
{
sunTypePercent = 70.0;
}
// calculate CIV Value
float civNum,civNum1,civNum2,civNum3,civNum4,civNum5;
civNum1 = sunTypePercent/100;
civNum2 = plasma + particle;
civNum3 = civNum2/200;
civNum4 = civNum1 - civNum3;
civNum5 = earth + moons;
civNum = civNum4 * civNum5;
return civNum;
}
//display data
void LocationData::displaydata()
{
cout << "suntype: " << sunType << endl;
cout << "earth: " << noOfEarthLikePlanets << endl;
cout << "moons: " << noOfEarthLikeMoons << endl;
cout << "particle: " << aveParticulateDensity << endl;
cout <<"density: " << avePlasmaDensity << endl;
cout << "CIV value: " << computeCivIndex()<< endl;
}
這是我遇到問題的實際代碼。對於computeCivIndex()函數,它實際上是位於我的LocationData.h文件中的公共LocationData類下的一個靜態,它看起來像這樣。
static float compute CivIndex(string st,int earth,int moons,float particle,float plasma);
所以爲了從函數中檢索值,我應該這樣做呢?
COUT < < 「CIV值:」 < < LocationData.computeCivIndex()< < ENDL;
'civNum'未初始化。 –
但你正在返回'civNum',而不是函數中的任何值... – 0x499602D2
什麼是'civNum',它是如何在'computeCivIndex()'中修改的?添加相關代碼。 – Kunal