Problem about Static

U

utab

Dear all,

I have a little problem about static class members and could not find
that though it is very easy. Could you please help me?

#include <iostream>

using namespace std;

class SavingsAccount{
public:
SavingsAccount(double amount){
savingsBalance=amount;
}
void calculateMonthlyInterest(){
double mi; // monthly interest
mi=savingsBalance*annualInterestRate/12;
savingsBalance+=mi;
}
static void modifyInterestRate(double newrate){
annualInterestRate = newrate;
}
double savingsGet() const{
return savingsBalance;
}
static void setannualInterestRate(double rate){
annualInterestRate=rate;
}
private:
static double annualInterestRate;
double savingsBalance;
};

int main()
{
SavingsAccount saver1(2000.0);
SavingsAccount saver2(3000.0);

SavingsAccount::setannualInterestRate(3.0);

saver1.calculateMonthlyInterest();
saver2.calculateMonthlyInterest();

cout << saver1.savingsGet() << endl;
cout << saver2.savingsGet() << endl;

SavingsAccount::modifyInterestRate(4.0);

saver1.calculateMonthlyInterest();
saver2.calculateMonthlyInterest();

cout << saver1.savingsGet() << endl;
cout << saver2.savingsGet() << endl;

return 0;

}
 
T

TB

utab skrev:
Dear all,

I have a little problem about static class members and could not find
that though it is very easy. Could you please help me?

And the problem is?
#include <iostream>

using namespace std;

class SavingsAccount{
public:
SavingsAccount(double amount){
savingsBalance=amount;
}
void calculateMonthlyInterest(){
double mi; // monthly interest
mi=savingsBalance*annualInterestRate/12;
savingsBalance+=mi;
}
static void modifyInterestRate(double newrate){
annualInterestRate = newrate;
}
double savingsGet() const{
return savingsBalance;
}
static void setannualInterestRate(double rate){
annualInterestRate=rate;
}
private:
static double annualInterestRate;
double savingsBalance;
};

double SavingsAccount::annualInterestRate = 0.0;

<snip>
 
U

utab

Sorry for that the problem is

/tmp/ccBiUko3.o: In function
`SavingsAccount::calculateMonthlyInterest()':
d2.cc:(.gnu.linkonce.t._ZN14SavingsAccount24calculateMonthlyInterestEv[SavingsAccount::calculateMonthlyInterest()]+0xd):
undefined reference to `SavingsAccount::annualInterestRate'
/tmp/ccBiUko3.o: In function
`SavingsAccount::modifyInterestRate(double)':
d2.cc:(.gnu.linkonce.t._ZN14SavingsAccount18modifyInterestRateEd[SavingsAccount::modifyInterestRate(double)]+0x19):
undefined reference to `SavingsAccount::annualInterestRate'
d2.cc:(.gnu.linkonce.t._ZN14SavingsAccount18modifyInterestRateEd[SavingsAccount::modifyInterestRate(double)]+0x1f):
undefined reference to `SavingsAccount::annualInterestRate'
/tmp/ccBiUko3.o: In function
`SavingsAccount::setannualInterestRate(double)':
d2.cc:(.gnu.linkonce.t._ZN14SavingsAccount21setannualInterestRateEd[SavingsAccount::setannualInterestRate(double)]+0x19):
undefined reference to `SavingsAccount::annualInterestRate'
d2.cc:(.gnu.linkonce.t._ZN14SavingsAccount21setannualInterestRateEd[SavingsAccount::setannualInterestRate(double)]+0x1f):
undefined reference to `SavingsAccount::annualInterestRate'
collect2: ld returned 1 exit status

:))
 
M

Michiel.Salters

utab said:
Sorry for that the problem is

/tmp/ccBiUko3.o: In function
`SavingsAccount::calculateMonthlyInterest()':
d2.cc:(.gnu.linkonce.t._ZN14SavingsAccount24calculateMonthlyInterestEv[SavingsAccount::calculateMonthlyInterest()]+0xd):
undefined reference to `SavingsAccount::annualInterestRate'
(5 more places in which the same variable is referenced)

Well, as all the error messages say, the problem is simple.
SavingsAccount::annualInterestRate is undefined; so define it.

HTH,
Michiel Salters
 
T

TB

utab skrev:
Sorry for that the problem is

/tmp/ccBiUko3.o: In function
`SavingsAccount::calculateMonthlyInterest()':
d2.cc:(.gnu.linkonce.t._ZN14SavingsAccount24calculateMonthlyInterestEv[SavingsAccount::calculateMonthlyInterest()]+0xd):
undefined reference to `SavingsAccount::annualInterestRate'
/tmp/ccBiUko3.o: In function
`SavingsAccount::modifyInterestRate(double)':
d2.cc:(.gnu.linkonce.t._ZN14SavingsAccount18modifyInterestRateEd[SavingsAccount::modifyInterestRate(double)]+0x19):
undefined reference to `SavingsAccount::annualInterestRate'
d2.cc:(.gnu.linkonce.t._ZN14SavingsAccount18modifyInterestRateEd[SavingsAccount::modifyInterestRate(double)]+0x1f):
undefined reference to `SavingsAccount::annualInterestRate'
/tmp/ccBiUko3.o: In function
`SavingsAccount::setannualInterestRate(double)':
d2.cc:(.gnu.linkonce.t._ZN14SavingsAccount21setannualInterestRateEd[SavingsAccount::setannualInterestRate(double)]+0x19):
undefined reference to `SavingsAccount::annualInterestRate'
d2.cc:(.gnu.linkonce.t._ZN14SavingsAccount21setannualInterestRateEd[SavingsAccount::setannualInterestRate(double)]+0x1f):
undefined reference to `SavingsAccount::annualInterestRate'
collect2: ld returned 1 exit status

:))

Then define it as I did in my first reply.
 
B

Ben Pope

utab said:
how, if I had understood my problem I would not have asked :)) sorry

If you bothered to quote the replies, you would not only see the answer,
but a description of where the answer is.

Please quote what you are responding to.
Write below the bit you are directly responding to.
Snip anything that you do not want to respond directly to.

Ben Pope
 
U

utab

Thx Mr.Pope,

Now if I have made the declerations of class and implementations in
different .h and .cc files I could have declare

double SavingsAccount::annualInterestRate = 0 // in the header file for
class decleration

but here where do I have to that decleration. Since that is static I
think I have to use a static member function to reach that value. So my
confusion is static reach of the problem I guess. Or Maybe this is
wrong simply I have to seperate them to do something like this...

Thx.
 
T

TB

utab skrev:
Thx Mr.Pope,

Now if I have made the declerations of class and implementations in
different .h and .cc files I could have declare

double SavingsAccount::annualInterestRate = 0 // in the header file for
class decleration

but here where do I have to that decleration. Since that is static I
think I have to use a static member function to reach that value. So my
confusion is static reach of the problem I guess. Or Maybe this is
wrong simply I have to seperate them to do something like this...

Thx.

class A {
public:
// declaration
static int i;
};

// definition of A::i
int A::i;

int main() {
A::i = 8;
}
 
B

Ben Pope

utab said:
Thx Mr.Pope,

Now if I have made the declerations of class and implementations in
different .h and .cc files I could have declare

double SavingsAccount::annualInterestRate = 0 // in the header file for
class decleration

but here where do I have to that decleration. Since that is static I
think I have to use a static member function to reach that value. So my
confusion is static reach of the problem I guess. Or Maybe this is
wrong simply I have to seperate them to do something like this...

I have no idea what you're talking about.

The program you posted was complete except for the line above, which was
missing. TB told you where you could put it.

Just add the line somewhere after the class declaration and compile and
link it.

Ben Pope
 
D

Daniel T.

"utab said:
I have a little problem about static class members and could not find
that though it is very easy. Could you please help me?

class Foo {
static int bar;
};

The above declares a int named bar in the Foo class, where there will
only be one bar no matter how many Foos are created. *It doesn't define
the bar, it only declares it*.

Somewhere outside the class, you need to define bar, and you need to
make sure that it is only defined once throughout the entire program (IE
you can't put the definition in a header file that is included in more
than one source file.)

The definition would look like this:

int Foo::bar;

optionally (preferred) you can give it an initial value as well:

int Foo::bar = 0;
 
U

utab

Thanks DanielT and TB, no need to be furious Ben Pope :((

I appreciate your help and advice. One more to go, is there a special
issue in static data and member functions because you are declaring
them out of the class. So I have to read maybe carefully once more from
some sources.

you will soon see me in a code of bugs u can be sure of it.

Thanks to you all DanielT, TB and of course Ben Pope :))

c u all. Regards
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,774
Messages
2,569,596
Members
45,143
Latest member
DewittMill
Top