Reputation: 67
Sorry guys I'm new to C++ and I have this silly question to ask. how to I retrieve the value of civIndex from LocationData computeCivIndex function to PointTwoD class. does the GET function help in this situation?
LocationData.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 file
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();
}
so what should i include or what mistake have i made?
Upvotes: 0
Views: 7094
Reputation: 28762
The declaration of static float LocationData::civIndex;
means that civIndex
is static
and private
(*) in LocationData
.
(*) as the default access modifier
The private
access means that you cannot access it directly via a variable of type LocationData
if that class does not explicitly allow it. You have two options:
1) Provide a public getter function:
public static float LocationData::getCivIndex()
{
return civIndex;
}
Note: you will need to declare this function in the class definition as well:
class LocationData
{
// ...
public static float getCivIndex();
};
2) Make the access of civIndex
in LocationData
public.
The second option is not advised as that would allow any code to directly access and modify the value of civIndex
, which can easily lead to errors (and errors of these types are hard to track down in a large project). Even if you need a setter function that only sets the passed-in value, it is advisable to declare the variable private
as that forces other code to go through the public method, making it easier to identify what code is accessing the variable during debugging in case of an error.
You can use option 1) like this:
LocationData locationData;
locationdata.getCivIndex();
Or even without having a variable of type LocationData
, as the variable is static in that class:
LocationData::getCivIndex();
Upvotes: 2
Reputation: 6597
You can not directly access locationdata.civIndex
like you did. Why? Because you set civIndex
as a private
member of the LocationData
class.
There are a few solutions depending on your needs:
Make civIndex
a public
member of LocationData
or
Create a getter method to provide civIndex
such as
static float getCivIndex( )
{
return civIndex;
}
See:
Upvotes: 0
Reputation: 2744
computeCivIndex
is a static member, so the correct syntax is:
cout << "civ: " << LocationData::civindex << endl;
However, your civindex variable is private, so you have to create a getter:
static float getCivIndex()
{
return civIndex;
}
cout << "civ: " << LocationData::getCivIndex() << endl;
Upvotes: 1