linker error, using static member

This is a discussion on linker error, using static member within the C++ Programming forums, part of the General Programming Boards category; Hello guru's, >Studying C++: I am trying to understand static members in classes. I am getting a linker error when ...

  1. #1
    Registered User
    Join Date
    May 2003
    Posts
    4

    linker error, using static member

    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();
    }

  2. #2
    Registered User
    Join Date
    May 2003
    Posts
    148
    There is no memory reserved for amount
    Code:
    //now there is
    int Teller::amount = 0;

  3. #3
    Registered User
    Join Date
    May 2003
    Posts
    4
    This is done in Init_Amount , Amount = 0

    Or is this the wrong location to do this?

  4. #4
    ....
    Join Date
    Aug 2001
    Location
    Groningen (NL)
    Posts
    2,386
    A static variable is defined in the class, but must be declare outside the class. So outside your class you should declare it, for example like this:

    int Teller::amount = 0;

    This is because a class definition does not allocate space for the variable, the declaration outside the class does.

  5. #5
    Cat
    Cat is offline
    Registered User
    Join Date
    May 2003
    Posts
    1,571
    Shiro is right in his idea, although I think he has the declaration and definition backwards.

    A declaration allocates nothing, it only says (declares) to the compiler that something will exist.

    A definition allocates space. It tells the compiler exactly how the variable, etc. should be created (defined).

  6. #6
    Disturbed Boy gustavosserra's Avatar
    Join Date
    Apr 2003
    Posts
    244
    Originally posted by Shiro
    A static variable is defined in the class, but must be declare outside the class. So outside your class you should declare it, for example like this:

    int Teller::amount = 0;

    This is because a class definition does not allocate space for the variable, the declaration outside the class does.
    Mustnīt you put extern before the declaration?

    extern int Teller::amount = 0;
    Nothing more to tell about me...
    Happy day =)

  7. #7
    Toaster Zach L.'s Avatar
    Join Date
    Aug 2001
    Posts
    2,686
    >> Mustnīt you put extern before the declaration?

    'extern' indicates that what you have provided is simply a declaration, and not a definition (which has to be elsewhere). In this case, you have a definition, so if you were to use 'extern', then that keyword would be ignored by the compiler.

    You use 'extern' when you have a symbol which you need to use in multiple files to provide declarations (so that symbol is defined) without redefining it (which is illegal).
    The word rap as it applies to music is the result of a peculiar phonological rule which has stripped the word of its initial voiceless velar stop.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. compilation problems with static data member
    By viral612 in forum C++ Programming
    Replies: 1
    Last Post: 08-13-2008, 07:55 AM
  2. Static & Data Member
    By ecoliteracy in forum C++ Programming
    Replies: 1
    Last Post: 04-16-2007, 09:46 PM
  3. Replies: 28
    Last Post: 07-17-2006, 12:35 AM
  4. Static member functions more efficient?
    By drrngrvy in forum C++ Programming
    Replies: 6
    Last Post: 06-16-2006, 08:07 AM
  5. opengl program as win API menu item
    By SAMSAM in forum Game Programming
    Replies: 1
    Last Post: 03-03-2003, 07:48 PM

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