从另一个函数和类 C++ 获取值
getting value from another function and class c++
对不起,伙计们,我是C++新手,我有一个愚蠢的问题要问。 如何从位置数据 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);
};
位置数据.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;
}
点二分之一.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();
};
点二.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();
}
那么我应该包括什么或我犯了什么错误?
static float LocationData::civIndex;
的声明表示civIndex
在LocationData
中static
和private
(*)。
(*) 作为默认访问修饰符
private
访问意味着如果 LocationData
类型的变量未显式允许,则无法直接通过该变量访问它。 您有两种选择:
1)提供公共获取功能:
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();
computeCivIndex
是一个静态成员,所以正确的语法是:
cout << "civ: " << LocationData::civindex << endl;
但是,你的 civindex 变量是私有的,所以你必须创建一个 getter:
static float getCivIndex()
{
return civIndex;
}
cout << "civ: " << LocationData::getCivIndex() << endl;
您无法像以前那样直接访问locationdata.civIndex
。为什么?因为您将civIndex
设置为LocationData
类的private
成员。
根据您的需求,有几种解决方案:
让civIndex
成为LocationData
public
成员
或
创建一个 getter 方法来提供civIndex
例如
static float getCivIndex( )
{
return civIndex;
}
看:
C++ 公共
C++保护
C++私人
- C++为构建时间获取QDateTime的可靠方法
- lambda参数转换为constexpr技巧,然后获取带链接的数组
- 如何使用 < 和 > 命令获取 c++ 中的输入和输出?
- 使用指针从C++中的数组中获取最大值
- 如何获取std::result_of函数的返回类型
- 如何在openssl-ecc中获取十六进制格式的私钥
- 使用Unreal C++获取VR耳机的世界位置/方向
- 获取日期异步信号安全吗?如果在信号处理程序中使用,它会导致死锁吗
- 从C字符串中获取奇怪的字符串长度
- 为什么我的for循环不能正确获取argv
- 从python中调用C++函数并获取返回值
- 如何获取一个数字的前3位
- 获取字符串的长度并将其分配给数组
- 无法获取菜单选择以运行函数.C++
- 数组长度,为什么从命令行获取时不能使用它?
- Boost Spirit,获取迭代器内部语义动作
- 尝试通过OCI例程从Oracle获取blob数据,但出现错误:ORA-01008:并非所有变量都绑定
- 具有默认值的引用获取函数
- xmake总是报告:错误:无法获取cxx的程序,为什么
- 在c++中获取大文件的大小