Hello guru's,
>Studying C++: I am trying to understand static members in classes. I am getting a linker error when I try to compile the code below.
I am pretty sure the linker error is related to the memory definition or declaration of the static int.
Can someone explain me how to solve my problem, how to run this simple program without errors?
Thanks,
Mike
#include <iostream.h>
class Teller {
private:
int value;
static int amount;
public:
Teller( int W ) { value = W; amount++; }
// constructor, static variabele
static void init_amount() { amount = 0 ; }
int give_value() {return value; }
static int give_amount() { return amount; }
};
void main()
{
Teller::init_amount(); // Set 'amount' to 0
Teller T1(10); // Object T1
Teller T2(20); // Object T2
cout << "\nValue = " << T1.Teller::give_value() << " Amount = " << Teller::give_amount();
cout << "\nValue = " << T2.Teller::give_value()
<< " Amount = " << Teller::give_amount();
cout << "\nAmount through scope resolution operator = "
<< Teller::give_amount();
getchar();
}