static const class members

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

    static const class members


    Hi,



    I'm new to programming in C++ (using VC6) and ran into the following
    problem: I want to declare and define a class member variable as 'static
    const', but something seems to go wrong with the linking.



    I specify a class Port the following way:

    Port.h:

    class __declspec(dlle xport) Port

    { static const int IN_PORT;

    //...

    }



    Port.cpp:

    #include "Port.h"

    const int Port::IN_PORT=0 ;

    //...



    When I build my project containing this code, it's all ok. I can use
    this class as expected within the project.

    However, when I want to use this variable from another project and class
    (note that the Port-class is in a dll), I get a linker error:

    AddInt.obj : error LNK2001: unresolved external symbol "public: static
    int const Port::TYPE_CObj ect" (?TYPE_CObject@ Port@@2HB)



    I don't know excatly why this is a problem: I imported the Port.h-file
    and the whole Port-class was exported from the dll using
    __declspec(dlle xport). Maybe dll's and static class members need some
    sort of special treatment? Maybe, since the source only includes Port.h,
    I need to initialize the constant in Port.h?

    I wasn't sure about this, but tried to do this and ran into another
    problem. I used the following code and got the following error:



    Port.h:

    class __declspec(dlle xport) Port

    { static const int IN_PORT=0;

    //...

    }



    Port.cpp:

    #include "Port.h"

    //const int Port::IN_PORT=0 ;

    //...



    d:\programming\ c++\luctor\src\ pipeline\port.h (13) : error C2252:
    'IN_PORT' : pure specifier can only be specified for functions



    Apparently, VC assumes my beautiful IN_PORT variable is/wants to be a
    virtual function, because I define it as =0... Does anyone know why it
    assumes this? I have never used the 'virtual' keyword in my short, but
    exciting C++ career, so I have no clue why it starts whining about pure
    virtual function specifiers.



    Can anybody help me with this problem (actually, I have 2 problems:
    the usage of the static member from a dll and the initialisation of
    the static)?



    I won't post my complete source, because that's rather large, but I can
    mail it if anyone wants to have a look at it. Thanks in advance,



    Jan


    --
    Posted via http://dbforums.com
  • stephan beal

    #2
    Re: static const class members

    Jan13 wrote:[color=blue]
    > class __declspec(dlle xport) Port
    >
    > { static const int IN_PORT;[/color]

    AFAIK a const must be defined when it is declared:

    static const int IN_PORT = 0;

    but only integral types can be initialized that way, i believe.

    --
    ----- stephan beal
    Registered Linux User #71917 http://counter.li.org
    I speak for myself, not my employer. Contents may
    be hot. Slippery when wet. Reading disclaimers makes
    you go blind. Writing them is worse. You have been Warned.

    Comment

    • WW

      #3
      Re: static const class members

      Jan13 wrote:[color=blue]
      > I specify a class Port the following way:
      >
      > Port.h:
      >
      > class __declspec(dlle xport) Port
      >
      > { static const int IN_PORT;
      >
      > }
      >
      > Port.cpp:
      >
      > #include "Port.h"
      >
      > const int Port::IN_PORT=0 ;
      >
      > //...
      >
      >
      >
      > When I build my project containing this code, it's all ok. I can use
      > this class as expected within the project.
      >
      > However, when I want to use this variable from another project and
      > class (note that the Port-class is in a dll), I get a linker error:
      >
      > AddInt.obj : error LNK2001: unresolved external symbol "public: static
      > int const Port::TYPE_CObj ect" (?TYPE_CObject@ Port@@2HB)[/color]

      While this is completely off-topic, I will give my guess:

      __declspec(dlle xport) const int Port::IN_PORT=0 ;

      You need to export the variable itself, because its name does not seem
      to be exported.
      [color=blue]
      > I wasn't sure about this, but tried to do this and ran into another
      > problem. I used the following code and got the following error:
      >
      > Port.h:
      >
      > class __declspec(dlle xport) Port
      >
      > { static const int IN_PORT=0;[/color]

      This is an error in VC6. You need to say:

      enum {
      IN_PORT=0
      };

      White Wolf


      Comment

      • jeffc

        #4
        Re: static const class members


        "Jan13" <member44596@db forums.com> wrote in message
        news:3492831.10 66388874@dbforu ms.com...[color=blue]
        >
        > Apparently, VC assumes my beautiful IN_PORT variable is/wants to be a
        > virtual function, because I define it as =0... Does anyone know why it
        > assumes this? I have never used the 'virtual' keyword in my short, but
        > exciting C++ career, so I have no clue why it starts whining about pure
        > virtual function specifiers.[/color]

        That's just a manifestation of the same problem. You have a VC++ linkage
        problem, not a C++ problem. Try the microsoft.publi c.vc.* newsgroups - they
        will know better.


        Comment

        Working...