抱歉,我是 C++ 新手,我有一个愚蠢的问题要问。如何从 LocationData computeCivIndex 函数中检索 civIndex 的值到 PointTwoD 类。GET 功能在这种情况下有帮助吗?
位置数据.h
class LocationData
{
private:
string sunType;
int noOfEarthLikePlanets, noOfEarthLikeMoons;
float aveParticulateDensity, avePlasmaDensity;
static float civIndex;
public:
LocationData(); //default constructor
LocationData(string, int, int, float, float); // no default constructor
void setLocationData(string, int, int, float, float);
void displaydata();
static float computeCivIndex(string st, int earth, int moons, float particle, float plasma);
};
位置数据imp.cpp
float LocationData::civIndex = 0;
//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;
civIndex = civNum;
//return civNum;
}
pointtwod.h 文件
class PointTwoD
{
private:
int xcord,ycord;
float civIndex;
LocationData locationdata;
public:
PointTwoD();
PointTwoD(int, int, string, int, int, float, float, float);
string toString();
void displayPointdata();
};
pointtwod.cpp
void PointTwoD::displayPointdata()
{
PointTwoD pointtwod;
LocationData locationdata;
cout << "X axis: " << xcord << endl;
cout << "Y axis: " << ycord << endl;
cout << "civ: " << locationdata.civIndex << endl;
//locationdata.displaydata();
}
那么我应该包括什么或我犯了什么错误?