static members

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

    static members

    The following code compiles fine, however when I try to execute it, I get
    this:
    main.obj : error LNK2001: unresolved external symbol "private: static int
    Class::dI" (?dI@Class@@0HA )
    It works fine when I do not use dI.

    #include<iostre am.h>

    class Class{
    static int dI;
    public:
    int I;
    static void init();
    };

    void Class::init(){
    if(!(dI)){
    dI = 100;
    }
    }

    void main(){
    Class a;
    Class::init();
    a.I = 2;
    cout<<a.I;
    }

    Thanks in advance,
    Lem



  • Rolf Magnus

    #2
    Re: static members

    Lem wrote:
    [color=blue]
    > The following code compiles fine, however when I try to execute it, I
    > get this:
    > main.obj : error LNK2001: unresolved external symbol "private: static
    > int Class::dI" (?dI@Class@@0HA )
    > It works fine when I do not use dI.[/color]
    [color=blue]
    > #include<iostre am.h>[/color]

    This header is non-standard. Use <iostream> instead.
    [color=blue]
    > class Class{
    > static int dI;
    > public:
    > int I;
    > static void init();
    > };[/color]

    The problem is that you declare dI, but you don't define it anywhere. A
    non-static member variable is instantiated when an object of the class
    is created, but a static member must be defined explicitly. So add:

    int Class:dI;
    [color=blue]
    > void Class::init(){
    > if(!(dI)){
    > dI = 100;
    > }
    > }
    >
    > void main(){[/color]

    main() must return int.
    [color=blue]
    > Class a;
    > Class::init();
    > a.I = 2;
    > cout<<a.I;[/color]

    If you use <iostream>, you need to change that into:

    std::cout<<a.I;
    [color=blue]
    > }
    >
    > Thanks in advance,
    > Lem[/color]

    --
    "Ziel des Spieles ist es, Länder zu überfallen, Kontinente und die
    Welt zu erobern, sowie Gegner zu schlagen." -- Risiko 1976
    "Ziel des Spieles ist es, Länder und Kontinente von Besatzungsarmee n
    zu befreien und in die Unabhängigkeit zu führen." -- Risiko 1990

    Comment

    • Nick Hounsome

      #3
      Re: static members


      "Lem" <hjlem@pacific. net.sg> wrote in message
      news:c1sgs5$phc $2@nobel2.pacif ic.net.sg...[color=blue]
      > The following code compiles fine, however when I try to execute it, I get
      > this:
      > main.obj : error LNK2001: unresolved external symbol "private: static int
      > Class::dI" (?dI@Class@@0HA )
      > It works fine when I do not use dI.
      >
      > #include<iostre am.h>
      >
      > class Class{
      > static int dI;
      > public:
      > int I;
      > static void init();
      > };
      >
      > void Class::init(){
      > if(!(dI)){
      > dI = 100;
      > }
      > }
      >
      > void main(){
      > Class a;
      > Class::init();
      > a.I = 2;
      > cout<<a.I;
      > }
      >
      > Thanks in advance,
      > Lem[/color]

      int Class::dI = 100;

      and you don't need the init function either!

      (and you probably wanted and endl in the output.)


      Comment

      Working...