从另一个函数和类c++; 对不起,我是C++新手,我有这个愚蠢的问题要问。如何从LocationData computeCivIndex函数到PointTwoD类检索civIndex的值。GET函数在这种情况下有帮助吗
位置数据.h从另一个函数和类c++; 对不起,我是C++新手,我有这个愚蠢的问题要问。如何从LocationData computeCivIndex函数到PointTwoD类检索civIndex的值。GET函数在这种情况下有帮助吗,c++,C++,位置数据.h class LocationData { private: string sunType; int noOfEarthLikePlanets, noOfEarthLikeMoons; float aveParticulateDensity, avePlasmaDensity; static float civIndex; public: LocationData(); //default constructor Locat
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);
};
locationdataimp.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();
}
void PointTwoD::displayPointdata()
{
两点两点两点;
位置数据位置数据;
coutcomputeCivIndex
是一个静态成员,因此正确的语法是:
cout << "civ: " << LocationData::civindex << endl;
coutcomputeCivIndex
是一个静态成员,因此正确的语法是:
cout << "civ: " << LocationData::civindex << endl;
coutcomputeCivIndex
是一个静态成员,因此正确的语法是:
cout << "civ: " << LocationData::civindex << endl;
coutcomputeCivIndex
是一个静态成员,因此正确的语法是:
cout << "civ: " << LocationData::civindex << endl;
cout您不能像以前那样直接访问locationdata.civIndex
。为什么?因为您将civIndex
设置为locationdata
类的private
成员
根据您的需要,有几种解决方案:
使civIndex
apublic
成为LocationData
或
创建一个getter方法来提供civIndex
,例如
static float getCivIndex( )
{
return civIndex;
}
见:
您不能像以前那样直接访问locationdata.civIndex
。为什么?因为您将civIndex
设置为locationdata
类的private
成员
根据您的需要,有几种解决方案:
使civIndex
apublic
成为LocationData
或
创建一个getter方法来提供civIndex
,例如
static float getCivIndex( )
{
return civIndex;
}
见:
您不能像以前那样直接访问locationdata.civIndex
。为什么?因为您将civIndex
设置为locationdata
类的private
成员
根据您的需要,有几种解决方案:
使civIndex
apublic
成为LocationData
或
创建一个getter方法来提供civIndex
,例如
static float getCivIndex( )
{
return civIndex;
}
见:
您不能像以前那样直接访问locationdata.civIndex
。为什么?因为您将civIndex
设置为locationdata
类的private
成员
根据您的需要,有几种解决方案:
使civIndex
apublic
成为LocationData
或
创建一个getter方法来提供civIndex
,例如
static float getCivIndex( )
{
return civIndex;
}
见:
静态浮点位置数据::civIndex;
的声明意味着civIndex
在LocationData
中是static
和private
(*)
(*)作为默认访问修饰符
private
访问意味着您不能通过LocationData
类型的变量直接访问它,如果该类不明确允许它。您有两个选项:
1) 提供公共getter函数:
public static float LocationData::getCivIndex()
{
return civIndex;
}
注意:您还需要在类定义中声明此函数:
class LocationData
{
// ...
public static float getCivIndex();
};
2) 在LocationData
中公开访问civIndex
不建议使用第二个选项,因为这将允许任何代码直接访问和修改civIndex
的值,这很容易导致错误(在大型项目中很难跟踪这些类型的错误)。即使您需要一个仅设置传入值的setter函数,也建议声明变量private
,因为这会强制其他代码通过public方法,以便在出现错误时更容易识别调试期间访问变量的代码
您可以这样使用选项1):
LocationData locationData;
locationdata.getCivIndex();
或者即使没有类型为LocationData
的变量,因为该类中的变量是静态的:
LocationData::getCivIndex();
static float LocationData::civIndex;
的声明意味着civIndex
在LocationData
中是static
和private
(*)
(*)作为默认访问修饰符
private
访问意味着您不能通过LocationData
类型的变量直接访问它,如果该类不明确允许它。您有两个选项:
1) 提供公共getter函数:
public static float LocationData::getCivIndex()
{
return civIndex;
}
注意:您还需要在类定义中声明此函数:
class LocationData
{
// ...
public static float getCivIndex();
};
2) 在LocationData
中公开访问civIndex
不建议使用第二个选项,因为这将允许任何代码直接访问和修改civIndex
的值,这很容易导致错误(在大型项目中很难跟踪这些类型的错误)。即使您需要一个仅设置传入值的setter函数,也建议声明变量private
,因为这会强制其他代码通过public方法,以便在出现错误时更容易识别调试期间访问变量的代码
您可以这样使用选项1):
LocationData locationData;
locationdata.getCivIndex();
或者即使没有类型为LocationData
的变量,因为该类中的变量是静态的:
LocationData::getCivIndex();
static float LocationData::civIndex;
的声明意味着civIndex
在LocationData
中是static
和private
(*)
(*)作为默认访问修饰符
private
访问意味着您不能通过LocationData
类型的变量直接访问它,如果该类不明确允许它。您有两个选项:
1) 提供公共getter函数:
public static float LocationData::getCivIndex()
{
return civIndex;
}
注意:您还需要在类定义中声明此函数:
class LocationData
{
// ...
public static float getCivIndex();
};
2) 访问civIndex