Static members of a class

I am experimenting with static members of a class and getting some build issues

I have defined the simple class
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
class Account {

public:
		// interface functions here
		void applyint() { amount += amount * interestRate; }

		//Static object are the same for all class objects
		static double rate() { return interestRate; }

		static void rate(double ir){
			interestRate=ir;     // sets a new rate
		}


		private:
		std::string owner;
		double amount;

		//Static object are the same for all class objects
		//Instead, there is a single interestRate
		//object that is shared by all objects of type Account
		static double interestRate;
		static double initRate();
};


and initialise as

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20

Account ac1;
	Account *ac2 = &ac1;

	// define and initialize static class member
	// double Account::interestRate = initRate();

    // Setting global interest rate
	ac1.rate(0.06);

	// the functions below retrieve the global rate
	// equivalent ways to call the static member rate function
	double rate;
	rate = ac1.rate(); // through an Account object or reference
	rate = ac2->rate(); // through a pointer to an Account object
	//rate = Account::rate(); // directly from the class using the scope operator

	cout<<ac1.rate()<<'\n';
	cout<<ac2->rate()<<'\n';


when i build i am getting the following error:

main.o:G:\eclipse_workspace\cpp_lippman_5_class\Debug/..//Account.hpp:21: undefined reference to `Account::interestRate'
main.o:G:\eclipse_workspace\cpp_lippman_5_class\Debug/..//Account.hpp:21: undefined reference to `Account::interestRate'
main.o:G:\eclipse_workspace\cpp_lippman_5_class\Debug/..//Account.hpp:24: undefined reference to `Account::interestRate'
main.o:G:\eclipse_workspace\cpp_lippman_5_class\Debug/..//Account.hpp:24: undefined reference to `Account::interestRate'

when i remove the static keyword the project builds fine

pls can you advise?

tnks
christos
a piece of cake, add following lines out of any class or function you defined:

double Account::interestRate;
double Account::initRate() {};

then, it will work fine.
I have already tried that before

1
2
double Account::interestRate;
double Account::initRate();


when I initialise i get an error:


..\main.cpp: In function 'int main()':
..\main.cpp:24:18: error: invalid use of qualified-name 'Account::interestRate'
..\main.cpp:25:27: error: invalid use of qualified-name 'Account::initRate'
Build error occurred, build is stopped
hi anyone to give me some advise here?
Try to initialize interestRate in Account.cpp file :
double Account::interestRate = 0;
closed account (zb0S216C)
Define the body of both Account::rate( ) methods outside the class definition. As Ivan said, it's recommended that you place your definitions within a source module.

By the way, you may not know it, but the compiler will probably implicitly in-line both of those Account::rate( ) methods, assuming you have the correct optimization flags set :)

Wazzak
tnks for your help

thus static member class variables need proper intialization outside the class header file

tnks again

christos
Topic archived. No new replies allowed.