473,378 Members | 1,351 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,378 software developers and data experts.

Problem about Static


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;

}

Feb 20 '06 #1
12 1627
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>

--
TB @ SWEDEN
Feb 20 '06 #2
Sorry for that the problem is

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

:))

Feb 20 '06 #3

utab wrote:
Sorry for that the problem is

/tmp/ccBiUko3.o: In function
`SavingsAccount::calculateMonthlyInterest()':
d2.cc:(.gnu.linkonce.t._ZN14SavingsAccount24calcul ateMonthlyInterestEv[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

Feb 20 '06 #4
TB
utab skrev:
Sorry for that the problem is

/tmp/ccBiUko3.o: In function
`SavingsAccount::calculateMonthlyInterest()':
d2.cc:(.gnu.linkonce.t._ZN14SavingsAccount24calcul ateMonthlyInterestEv[SavingsAccount::calculateMonthlyInterest()]+0xd):
undefined reference to `SavingsAccount::annualInterestRate'
/tmp/ccBiUko3.o: In function
`SavingsAccount::modifyInterestRate(double)':
d2.cc:(.gnu.linkonce.t._ZN14SavingsAccount18modify InterestRateEd[SavingsAccount::modifyInterestRate(double)]+0x19):
undefined reference to `SavingsAccount::annualInterestRate'
d2.cc:(.gnu.linkonce.t._ZN14SavingsAccount18modify InterestRateEd[SavingsAccount::modifyInterestRate(double)]+0x1f):
undefined reference to `SavingsAccount::annualInterestRate'
/tmp/ccBiUko3.o: In function
`SavingsAccount::setannualInterestRate(double)':
d2.cc:(.gnu.linkonce.t._ZN14SavingsAccount21setann ualInterestRateEd[SavingsAccount::setannualInterestRate(double)]+0x19):
undefined reference to `SavingsAccount::annualInterestRate'
d2.cc:(.gnu.linkonce.t._ZN14SavingsAccount21setann ualInterestRateEd[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.

--
TB @ SWEDEN
Feb 20 '06 #5
how, if I had understood my problem I would not have asked :)) sorry

Feb 20 '06 #6
It is static Pay attention please,

you are making a redecleration I guess :(.

thx

Feb 20 '06 #7
utab wrote:
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
--
I'm not just a number. To many, I'm known as a string...
Feb 20 '06 #8
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.

Feb 20 '06 #9
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;
}

--
TB @ SWEDEN
Feb 20 '06 #10
utab wrote:
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
--
I'm not just a number. To many, I'm known as a string...
Feb 20 '06 #11
In article <11**********************@g47g2000cwa.googlegroups .com>,
"utab" <um********@gmail.com> wrote:
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;

--
Magic depends on tradition and belief. It does not welcome observation,
nor does it profit by experiment. On the other hand, science is based
on experience; it is open to correction by observation and experiment.
Feb 20 '06 #12
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

Feb 20 '06 #13

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Vinay Aggarwal | last post by:
I have been thinking about the lazy initialization and double checked locking problem. This problem is explain in detail here http://www.cs.umd.edu/~pugh/java/memoryModel/DoubleCheckedLocking.html...
49
by: Mark Hahn | last post by:
As we are addressing the "warts" in Python to be fixed in Prothon, we have come upon the mutable default parameter problem. For those unfamiliar with the problem, it can be seen in this Prothon...
3
by: Omer van Kloeten | last post by:
The Top Level Design: The class Base is a factory class with a twist. It uses the Assembly/Type classes to extract all types that inherit from it and add them to the list of types that inherit...
2
by: ajikoe | last post by:
Hi, I tried to follow the example in swig homepage. I found error which I don't understand. I use bcc32, I already include directory where my python.h exist in bcc32.cfg. /* File : example.c...
3
by: dice | last post by:
Hi, In order to use an external api call that requires a function pointer I am currently creating static wrappers to call my objects functions. I want to re-jig this so I only need 1 static...
6
by: toton | last post by:
Hi, If I have a singleton class based on dynamic initialization (with new ) , is it considered a memory leak? Anything in C++ standard says about it ? And little off - topic question , If the...
9
by: weidongtom | last post by:
Hi, I've written the code that follows, and I use the function add_word(), it seems to work fine *before* increase_arrays() is called that uses realloc() to allocate more memory to words. But...
2
by: Jeff | last post by:
Hey ..NET 2.0 I'm developing an application which will perform some webservice calls and I believe having those calls in a separate thread may help the app run smoother No user are waiting...
1
by: Alex Vinokur | last post by:
Hi, I have compilation problem on SUN CC compiler with template while using option -m64 (64-bit addressing model). No problem while using option -m32 (32-bit addressing model) $ CC -V CC:...
8
by: Stefano Sabatini | last post by:
Hi all, I'm encountering this while trying to implement a factory singleton method to generate objects. The singleton has a static map which binds a static creation function defined in each...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.