Static Member Variable?

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Leo

    Static Member Variable?

    I have a member function in this class, which needs a variable to keep
    track its status. Thus I used a static member variable.

    But I encounter a "unresolved external variable" error during compile
    (for the variable a).

    Can anyone tell me what I did wrong?


    ////////////// testclass.h
    #pragma once

    class testclass
    {

    public:

    // constructor and destructor
    testclass(void) ;
    ~testclass(void );

    // static part
    static int a;
    static void test(void);

    };




    ////////////// testclass.cpp

    #include "testclass. h"

    testclass::test class(void)
    {
    }

    testclass::~tes tclass(void)
    {
    }

    void testclass::test (void)
    {
    // problem appears after I put this line
    a=1;
    }

  • Ron Natalie

    #2
    Re: Static Member Variable?


    "Leo" <thepcprojects@ rogers.com> wrote in message news:uAZFb.7114 8$2We1.14091@ne ws04.bloor.is.n et.cable.rogers .com...
    [color=blue]
    > But I encounter a "unresolved external variable" error during compile
    > (for the variable a).[/color]

    You don't define the variable testclass::a anywhere. You get away with it
    as long as nobody uses it.

    in testclass.cpp (after the #include)
    put
    int testclass::a;

    You can give it an initializer if the default (zero) is not appropriate.

    Comment

    • Calvin Lai

      #3
      Re: Static Member Variable?

      But how come this is not necessary for non-static members? (that we just
      need to *declare* them in the header file);

      "Ron Natalie" <ron@sensor.com > wrote in message
      news:3fe8664a$0 $60605$9a6e19ea @news.newshosti ng.com...[color=blue]
      >
      > "Leo" <thepcprojects@ rogers.com> wrote in message[/color]
      news:uAZFb.7114 8$2We1.14091@ne ws04.bloor.is.n et.cable.rogers .com...[color=blue]
      >[color=green]
      > > But I encounter a "unresolved external variable" error during compile
      > > (for the variable a).[/color]
      >
      > You don't define the variable testclass::a anywhere. You get away with it
      > as long as nobody uses it.
      >
      > in testclass.cpp (after the #include)
      > put
      > int testclass::a;
      >
      > You can give it an initializer if the default (zero) is not appropriate.
      >[/color]


      Comment

      • Victor Bazarov

        #4
        Re: Static Member Variable?

        "Calvin Lai" <calvin.lai@i01 0.com> wrote...[color=blue]
        > But how come this is not necessary for non-static members? (that we just
        > need to *declare* them in the header file);[/color]

        Because you "define" them when you define an instance of the class.
        They are created along with an object of the class. Heck, an object
        of the class _consists_ of them, without them it wouldn't be.

        Please don't top-post.
        [color=blue]
        >
        > "Ron Natalie" <ron@sensor.com > wrote in message
        > news:3fe8664a$0 $60605$9a6e19ea @news.newshosti ng.com...[color=green]
        > >
        > > "Leo" <thepcprojects@ rogers.com> wrote in message[/color]
        > news:uAZFb.7114 8$2We1.14091@ne ws04.bloor.is.n et.cable.rogers .com...[color=green]
        > >[color=darkred]
        > > > But I encounter a "unresolved external variable" error during compile
        > > > (for the variable a).[/color]
        > >
        > > You don't define the variable testclass::a anywhere. You get away with[/color][/color]
        it[color=blue][color=green]
        > > as long as nobody uses it.
        > >
        > > in testclass.cpp (after the #include)
        > > put
        > > int testclass::a;
        > >
        > > You can give it an initializer if the default (zero) is not appropriate.
        > >[/color]
        >
        >[/color]


        Comment

        • Jeff Schwab

          #5
          Re: Static Member Variable?

          Calvin Lai wrote:[color=blue]
          > But how come this is not necessary for non-static members? (that we just
          > need to *declare* them in the header file);[/color]

          "Define" is being used here to mean "allocate storage." Storage for
          non-static member variables is provided as part of each instance of the
          class. Since static member variables by definition do not correspond to
          any instance, you have to state explicitly where they should be stored.

          Comment

          • Ron Natalie

            #6
            Re: Static Member Variable?


            "Calvin Lai" <calvin.lai@i01 0.com> wrote in message news:S60Gb.7314 6$2We1.27510@ne ws04.bloor.is.n et.cable.rogers .com...[color=blue]
            > But how come this is not necessary for non-static members? (that we just
            > need to *declare* them in the header file);[/color]

            Because they are created with each object as it is created (it's part of the object).
            The non-static members have to be put some where exactly once.

            Comment

            Working...