static error

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

    static error

    Hi there,
    I get a linker error with the following, can somebody tell me how to remedy
    it? the error is:
    --------------------Configuration: WinGalaga - Win32
    Release--------------------
    Linking...
    Weapon.obj : error LNK2001: unresolved external symbol "protected: static
    int Weapon::mCurrLi ght" (?mCurrLight@We apon@@1HA)
    WeaponManager.o bj : error LNK2001: unresolved external symbol "protected:
    static int Weapon::mCurrLi ght" (?mCurrLight@We apon@@1HA)
    Release/WinGalaga.exe : fatal error LNK1120: 1 unresolved externals
    Error executing link.exe.

    WinGalaga.exe - 3 error(s), 0 warning(s)


    I am trying to initialise the lights from the WeaponManager by:
    Weapon::InitLig hts();


    Here is the header of the file where the problem exists


    class Weapon : public GameEntity
    {
    friend class WeaponManager;

    public:
    Weapon(void);
    virtual ~Weapon(void);

    virtual void Draw(void);
    void DrawLight(void) ;
    static void InitLights(void ){mCurrLight = GL_LIGHT1;}

    ...

    protected:
    ...
    // light stuff
    static int mCurrLight;
    };


    Thanks
    Allan


  • Karl Heinz Buchegger

    #2
    Re: static error



    Allan Bruce wrote:[color=blue]
    >
    > Here is the header of the file where the problem exists
    >
    > class Weapon : public GameEntity
    > {
    > friend class WeaponManager;
    >
    > public:
    > Weapon(void);
    > virtual ~Weapon(void);
    >
    > virtual void Draw(void);
    > void DrawLight(void) ;
    > static void InitLights(void ){mCurrLight = GL_LIGHT1;}
    >
    > ...
    >
    > protected:
    > ...
    > // light stuff
    > static int mCurrLight;
    > };[/color]

    this *declares* a variable mCurrLight. But it does not *define* it.
    In other words: the above says: Somewhere there is a variable
    called Weapon::mCurrLi ght and it has type int.
    But where is that variable?
    You have to define it somewhere, eg. in Weapon.cpp

    #include "Weapon.h"

    int Weapon::mCurrLi ght; // Here it is!!!

    Weapon::Weapon( )
    {
    ...


    Your textbook should have a section on statis class members. Doesn't it?

    --
    Karl Heinz Buchegger
    kbuchegg@gascad .at

    Comment

    • Allan Bruce

      #3
      Re: static error


      "Karl Heinz Buchegger" <kbuchegg@gasca d.at> wrote in message
      news:3FAB73CD.3 5A09455@gascad. at...[color=blue]
      >
      >
      > Allan Bruce wrote:[color=green]
      > >
      > > Here is the header of the file where the problem exists
      > >
      > > class Weapon : public GameEntity
      > > {
      > > friend class WeaponManager;
      > >
      > > public:
      > > Weapon(void);
      > > virtual ~Weapon(void);
      > >
      > > virtual void Draw(void);
      > > void DrawLight(void) ;
      > > static void InitLights(void ){mCurrLight = GL_LIGHT1;}
      > >
      > > ...
      > >
      > > protected:
      > > ...
      > > // light stuff
      > > static int mCurrLight;
      > > };[/color]
      >
      > this *declares* a variable mCurrLight. But it does not *define* it.
      > In other words: the above says: Somewhere there is a variable
      > called Weapon::mCurrLi ght and it has type int.
      > But where is that variable?
      > You have to define it somewhere, eg. in Weapon.cpp
      >
      > #include "Weapon.h"
      >
      > int Weapon::mCurrLi ght; // Here it is!!!
      >
      > Weapon::Weapon( )
      > {
      > ...
      >
      >
      > Your textbook should have a section on statis class members. Doesn't it?
      >[/color]

      Thanks, thats it working now.

      My book does have a section on static members, but it is not explained very
      well. I understand why you need the inclusion of Weapon::mCurrLi ght;

      Thanks again
      Allan


      Comment

      • Frank Schmitt

        #4
        Re: static error

        "Allan Bruce" <allanmb@TAKEAW AYf2s.com> writes:
        [color=blue]
        > Hi there,
        > I get a linker error with the following, can somebody tell me how to remedy
        > it? the error is:
        > --------------------Configuration: WinGalaga - Win32
        > Release--------------------
        > Linking...
        > Weapon.obj : error LNK2001: unresolved external symbol "protected: static
        > int Weapon::mCurrLi ght" (?mCurrLight@We apon@@1HA)
        > WeaponManager.o bj : error LNK2001: unresolved external symbol "protected:
        > static int Weapon::mCurrLi ght" (?mCurrLight@We apon@@1HA)
        > Release/WinGalaga.exe : fatal error LNK1120: 1 unresolved externals
        > Error executing link.exe.
        >
        > WinGalaga.exe - 3 error(s), 0 warning(s)
        >
        >
        > I am trying to initialise the lights from the WeaponManager by:
        > Weapon::InitLig hts();
        >
        >
        > Here is the header of the file where the problem exists
        >
        >
        > class Weapon : public GameEntity
        > {
        > friend class WeaponManager;
        >
        > public:
        > Weapon(void);
        > virtual ~Weapon(void);
        >
        > virtual void Draw(void);
        > void DrawLight(void) ;
        > static void InitLights(void ){mCurrLight = GL_LIGHT1;}
        >
        > ...
        >
        > protected:
        > ...
        > // light stuff
        > static int mCurrLight;
        > };[/color]

        You have *declared* Weapon::mCurrLi ght, but you haven't *defined* it -
        just add

        int Weapon::mCurrLi ght;

        to Weapon.cpp

        HTH & kind regards
        frank

        --
        Frank Schmitt
        4SC AG phone: +49 89 700763-0
        e-mail: frankNO DOT SPAMschmitt AT 4sc DOT com

        Comment

        Working...