Jump to content

[C++] How do you modify a static variable via a static function?

Speedstack79
class SavingsAccount
{
public:
    static double annualInterestRate;
    int accountNumber;//from 1000 to 4999
.
.
.
};

.
.
.
static void SavingsAccount::modifyInterestRate(double percent)
{
    cout << "Monthly interest rate is to be changed to "  << percent << "%" << endl;
    annualInterestRate = percent;
}

I have tried various things, such as changing the line in the function to SavingsAccount::annualInterestRate = percent;, or removing the static in the function implementation, the errors go from cannot declare member function, or undefined reference....really not sure how to modify the value of the variable through the function, help is appreciated

person below me is a scrub

Link to comment
Share on other sites

Link to post
Share on other sites

7 minutes ago, d0nsen said:

ever thought about making the variable public?

 

http://stackoverflow.com/questions/14349877/static-global-variables-in-c

removing the static keyword? I cannot do that because this is an assignment and the assignment requires for this variable to use the static keyword

 

person below me is a scrub

Link to comment
Share on other sites

Link to post
Share on other sites

5 hours ago, Speedstack79 said:

removing the static keyword? I cannot do that because this is an assignment and the assignment requires for this variable to use the static keyword

Education... teaching good practices ready for industry...

MkCNa9w.jpg

The single biggest problem in communication is the illusion that it has taken place.

Link to comment
Share on other sites

Link to post
Share on other sites

9 hours ago, Speedstack79 said:

class SavingsAccount
{
public:
    static double annualInterestRate;
    int accountNumber;//from 1000 to 4999
.
.
.
};

.
.
.
static void SavingsAccount::modifyInterestRate(double percent)
{
    cout << "Monthly interest rate is to be changed to "  << percent << "%" << endl;
    annualInterestRate = percent;
}

I have tried various things, such as changing the line in the function to SavingsAccount::annualInterestRate = percent;, or removing the static in the function implementation, the errors go from cannot declare member function, or undefined reference....really not sure how to modify the value of the variable through the function, help is appreciated

The accepted answer made multiple changes to your code so I don't know if you now realize what the problem was and have learned anything?

 

When you declare a static class member in C++ there is no memory allocated for it - you need to define it separately. The reason is obvious, the member lives outside the class data in it's own little space. This line in the accepted answer is what fixed things:

double account::interestRate = 0.1; 

 

So your original code was fine, as such:

class SavingsAccount
{
public:
    static double annualInterestRate;
    int accountNumber;//from 1000 to 4999
.
.
.
};

.
double SavingsAccount::annualInterestRate = 0.1; //Define and initialise.  
.
static void SavingsAccount::modifyInterestRate(double percent)
{
    cout << "Monthly interest rate is to be changed to "  << percent << "%" << endl;
    annualInterestRate = percent;
}

 

Do note that non-const static data members are nothing more then glorified global variables, with all the evil that implies.

 

Link to comment
Share on other sites

Link to post
Share on other sites

  • 1 month later...
On 11/5/2016 at 3:45 AM, Unimportant said:

The accepted answer made multiple changes to your code so I don't know if you now realize what the problem was and have learned anything?

 

When you declare a static class member in C++ there is no memory allocated for it - you need to define it separately. The reason is obvious, the member lives outside the class data in it's own little space. This line in the accepted answer is what fixed things:


double account::interestRate = 0.1; 

 

So your original code was fine, as such:


class SavingsAccount
{
public:
    static double annualInterestRate;
    int accountNumber;//from 1000 to 4999
.
.
.
};

.
double SavingsAccount::annualInterestRate = 0.1; //Define and initialise.  
.
static void SavingsAccount::modifyInterestRate(double percent)
{
    cout << "Monthly interest rate is to be changed to "  << percent << "%" << endl;
    annualInterestRate = percent;
}

 

Do note that non-const static data members are nothing more then glorified global variables, with all the evil that implies.

 

Yea that line was the only thing that I changed on my source code

person below me is a scrub

Link to comment
Share on other sites

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×