Classes, static in private

closed account (EwCjE3v7)
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;
closed account (z0My6Up4)
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...

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
closed account (EwCjE3v7)
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
Topic archived. No new replies allowed.