Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/visual-studio-2008/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
从另一个函数和类c++; 对不起,我是C++新手,我有这个愚蠢的问题要问。如何从LocationData computeCivIndex函数到PointTwoD类检索civIndex的值。GET函数在这种情况下有帮助吗_C++ - Fatal编程技术网

从另一个函数和类c++; 对不起,我是C++新手,我有这个愚蠢的问题要问。如何从LocationData computeCivIndex函数到PointTwoD类检索civIndex的值。GET函数在这种情况下有帮助吗

从另一个函数和类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

位置数据.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);

};
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()
{
两点两点两点;
位置数据位置数据;

cout
computeCivIndex
是一个静态成员,因此正确的语法是:

cout << "civ: " << LocationData::civindex << endl;

cout
computeCivIndex
是一个静态成员,因此正确的语法是:

cout << "civ: " << LocationData::civindex << endl;

cout
computeCivIndex
是一个静态成员,因此正确的语法是:

cout << "civ: " << LocationData::civindex << endl;

cout
computeCivIndex
是一个静态成员,因此正确的语法是:

cout << "civ: " << LocationData::civindex << endl;

cout您不能像以前那样直接访问
locationdata.civIndex
。为什么?因为您将
civIndex
设置为
locationdata
类的
private
成员

根据您的需要,有几种解决方案:

使
civIndex
a
public
成为
LocationData

创建一个getter方法来提供
civIndex
,例如

  static float getCivIndex( )
  {
      return civIndex;
  }
见:


您不能像以前那样直接访问
locationdata.civIndex
。为什么?因为您将
civIndex
设置为
locationdata
类的
private
成员

根据您的需要,有几种解决方案:

使
civIndex
a
public
成为
LocationData

创建一个getter方法来提供
civIndex
,例如

  static float getCivIndex( )
  {
      return civIndex;
  }
见:


您不能像以前那样直接访问
locationdata.civIndex
。为什么?因为您将
civIndex
设置为
locationdata
类的
private
成员

根据您的需要,有几种解决方案:

使
civIndex
a
public
成为
LocationData

创建一个getter方法来提供
civIndex
,例如

  static float getCivIndex( )
  {
      return civIndex;
  }
见:


您不能像以前那样直接访问
locationdata.civIndex
。为什么?因为您将
civIndex
设置为
locationdata
类的
private
成员

根据您的需要,有几种解决方案:

使
civIndex
a
public
成为
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