static member variable

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

    static member variable

    given the following code:

    myclass.h:
    -------------------------------

    class CMyClass
    {
    public:
    CMyClass();
    private:
    static int test;
    }
    --------------------------------


    myclass.cpp:
    -------------------------------

    #include "myclass.h"

    CMyClass::CMyCl ass
    {
    CMyClass::test = 5; // error: unresolved external symbol
    }
    --------------------------------


    I want to make a private member variable of static kind. What did I make
    wrong? I always get the error message "unresolved external symbol".
    I've already tried to access it via "this->test" but this doesn't work
    either. Is it not allowed to access the static variable outside the same
    file? What do I have to do in order to be able to access it from within my
    member-functions?

    any help is appreciated,
    ekim


  • Iakov Nakhimovski

    #2
    Re: static member variable

    myclass.cpp should contain a line:

    int CMyClass::test = 5; // initial value for the static variable must be set
    separately

    It's ok to use the variable in the constructor (or any method) if you need
    this.

    Make sure you understand the difference between link & compile time errors
    for the future.

    HTH, Iakov

    "Ekim" <the.newsletter @gmx.net> wrote in message
    news:2p0c06Ffgf 5qU1@uni-berlin.de...[color=blue]
    > given the following code:
    >
    > myclass.h:
    > -------------------------------
    >
    > class CMyClass
    > {
    > public:
    > CMyClass();
    > private:
    > static int test;
    > }
    > --------------------------------
    >
    >
    > myclass.cpp:
    > -------------------------------
    >
    > #include "myclass.h"
    >
    > CMyClass::CMyCl ass
    > {
    > CMyClass::test = 5; // error: unresolved external[/color]
    symbol[color=blue]
    > }
    > --------------------------------
    >
    >
    > I want to make a private member variable of static kind. What did I make
    > wrong? I always get the error message "unresolved external symbol".
    > I've already tried to access it via "this->test" but this doesn't work
    > either. Is it not allowed to access the static variable outside the same
    > file? What do I have to do in order to be able to access it from within my
    > member-functions?
    >
    > any help is appreciated,
    > ekim
    >
    >[/color]


    Comment

    • Carlos Martinez Garcia

      #3
      Re: static member variable

      Hi Ekim:

      You have declare a static member variable and use it,
      but you haven't defined it.

      In your source put the definition after #include "myclass.h"
      int CMyClass::test= 0; //for example

      Ekim escribió:[color=blue]
      > given the following code:
      >
      > myclass.h:
      > -------------------------------
      >
      > class CMyClass
      > {
      > public:
      > CMyClass();
      > private:
      > static int test;
      > }
      > --------------------------------
      >
      >
      > myclass.cpp:
      > -------------------------------
      >
      > #include "myclass.h"
      >
      > CMyClass::CMyCl ass
      > {
      > CMyClass::test = 5; // error: unresolved external symbol
      > }
      > --------------------------------
      >
      >
      > I want to make a private member variable of static kind. What did I make
      > wrong? I always get the error message "unresolved external symbol".
      > I've already tried to access it via "this->test" but this doesn't work
      > either. Is it not allowed to access the static variable outside the same
      > file? What do I have to do in order to be able to access it from within my
      > member-functions?
      >
      > any help is appreciated,
      > ekim
      >
      >[/color]

      Comment

      • Ekim

        #4
        Re: static member variable

        problem solved - thx a lot to both of you!

        by ekim

        "Iakov Nakhimovski" <iakov.fake.add ress@hotmail.co m> wrote in message
        news:cgeu88$2j6 $1@news.island. liu.se...[color=blue]
        > myclass.cpp should contain a line:
        >
        > int CMyClass::test = 5; // initial value for the static variable must be[/color]
        set[color=blue]
        > separately
        >
        > It's ok to use the variable in the constructor (or any method) if you need
        > this.
        >
        > Make sure you understand the difference between link & compile time errors
        > for the future.
        >
        > HTH, Iakov
        >
        > "Ekim" <the.newsletter @gmx.net> wrote in message
        > news:2p0c06Ffgf 5qU1@uni-berlin.de...[color=green]
        > > given the following code:
        > >
        > > myclass.h:
        > > -------------------------------
        > >
        > > class CMyClass
        > > {
        > > public:
        > > CMyClass();
        > > private:
        > > static int test;
        > > }
        > > --------------------------------
        > >
        > >
        > > myclass.cpp:
        > > -------------------------------
        > >
        > > #include "myclass.h"
        > >
        > > CMyClass::CMyCl ass
        > > {
        > > CMyClass::test = 5; // error: unresolved external[/color]
        > symbol[color=green]
        > > }
        > > --------------------------------
        > >
        > >
        > > I want to make a private member variable of static kind. What did I make
        > > wrong? I always get the error message "unresolved external symbol".
        > > I've already tried to access it via "this->test" but this doesn't work
        > > either. Is it not allowed to access the static variable outside the same
        > > file? What do I have to do in order to be able to access it from within[/color][/color]
        my[color=blue][color=green]
        > > member-functions?
        > >
        > > any help is appreciated,
        > > ekim
        > >
        > >[/color]
        >
        >[/color]


        Comment

        Working...