static member variable

Discussion in 'C++' started by Carlos Martinez Garcia, Aug 24, 2004.

  1. 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ó:
    > given the following code:
    >
    > myclass.h:
    > -------------------------------
    >
    > class CMyClass
    > {
    > public:
    > CMyClass();
    > private:
    > static int test;
    > }
    > --------------------------------
    >
    >
    > myclass.cpp:
    > -------------------------------
    >
    > #include "myclass.h"
    >
    > CMyClass::CMyClass
    > {
    > 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
    >
    >
     
    Carlos Martinez Garcia, Aug 24, 2004
    #1
    1. Advertising

  2. 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" <> wrote in message
    news:...
    > given the following code:
    >
    > myclass.h:
    > -------------------------------
    >
    > class CMyClass
    > {
    > public:
    > CMyClass();
    > private:
    > static int test;
    > }
    > --------------------------------
    >
    >
    > myclass.cpp:
    > -------------------------------
    >
    > #include "myclass.h"
    >
    > CMyClass::CMyClass
    > {
    > 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, Aug 24, 2004
    #2
    1. Advertising

  3. Carlos Martinez Garcia

    Ekim Guest

    given the following code:

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

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


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

    #include "myclass.h"

    CMyClass::CMyClass
    {
    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
     
    Ekim, Aug 24, 2004
    #3
  4. Carlos Martinez Garcia

    Ekim Guest

    problem solved - thx a lot to both of you!

    by ekim

    "Iakov Nakhimovski" <> wrote in message
    news:cgeu88$2j6$...
    > 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" <> wrote in message
    > news:...
    > > given the following code:
    > >
    > > myclass.h:
    > > -------------------------------
    > >
    > > class CMyClass
    > > {
    > > public:
    > > CMyClass();
    > > private:
    > > static int test;
    > > }
    > > --------------------------------
    > >
    > >
    > > myclass.cpp:
    > > -------------------------------
    > >
    > > #include "myclass.h"
    > >
    > > CMyClass::CMyClass
    > > {
    > > 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
    > >
    > >

    >
    >
     
    Ekim, Aug 24, 2004
    #4
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. 0to60
    Replies:
    4
    Views:
    544
    jeffc
    Nov 21, 2003
  2. Sam
    Replies:
    4
    Views:
    491
    The Directive
    Jan 13, 2004
  3. Siemel Naran
    Replies:
    4
    Views:
    842
    Micah Cowan
    Jan 12, 2005
  4. aling
    Replies:
    6
    Views:
    507
    Xiaobin.Huang
    Oct 30, 2005
  5. dolphin
    Replies:
    3
    Views:
    1,403
    Pete Becker
    Dec 5, 2007
Loading...

Share This Page