Classes, static in private
May 12, 2014 at 9:02pm UTC
I cant get this to work, anyone know how? I get the following error:
error: ‘double Account::interestRate’ is private
static double interestRate;
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
class Account
{
public :
void calculate() { amount += amount * interestRate; }
static double rate() { return interestRate; }
static void rate(double );
private :
std::string owner;
double amount = 0.0;
static double interestRate;
static double initRate();
};
void Account::rate(double newRate)
{
interestRate = newRate;
}
static Account::interestRate = 4.0;
May 12, 2014 at 9:14pm UTC
Not sure but Is your code trying to directly access the private data member interestRate? It looks to me that this data member can only be accessed indirectly by calling one of the public functions...
May 12, 2014 at 9:16pm UTC
You need to create an object of class Account, then call its member rate to modify the private interestRate variable.
1 2 3 4 5 6 7 8 9
int main()
{
Account testAccount;
testAccount.rate(4.0);
return 0;
}
Last edited on May 12, 2014 at 9:16pm UTC
May 12, 2014 at 9:49pm UTC
Sorry guys, there were a few errors, fixed them. Thank you guys. Here is the updated code
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24
class Account
{
public :
void calculate() { amount += amount * interestRate; }
static double rate() { return interestRate; }
static void rate(double );
private :
std::string owner;
double amount = 0.0;
static double interestRate;
static double initRate();
};
void Account::rate(double newRate)
{
interestRate = newRate;
}
double Account::initRate()
{
return 4.0;
}
double Account::interestRate = initRate();
Last edited on May 12, 2014 at 9:49pm UTC
Topic archived. No new replies allowed.