从另一个函数和类 C++ 获取值

getting value from another function and class c++

本文关键字:C++ 获取 另一个 函数      更新时间:2023-10-16

对不起,伙计们,我是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; 的声明表示civIndexLocationDatastaticprivate (*)。

(*) 作为默认访问修饰符

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++私人