CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 8 of 8
  1. #1
    Join Date
    Aug 2002
    Location
    Hamburg / Germany
    Posts
    280

    Problem with static class-element

    Hi,
    I'm in some trouble again, and I bet someone of you has got the solution ;o)

    I built a class that keeps track of the existing instances of the class. I decided to have a static class member of type std::map the key should be some integer-value identifying the actual instance, the value should be a pointer to the object itself.

    The class looks like this:

    class MyClass
    {
    private:
    map<int,MyClass*> m_ClassMap;


    public:
    MyClass()

    ..
    ..

    }


    So far, so good. In the Constructor each of the instances shall register itself to the classmap. Therefore I put this inside the constructor:

    m_ClassMap[inst] = this;

    where inst is a value of type integer, for identifying a particular instance later.

    Ok,when I compile it I get a linker-error telling me, that there is an unresolved external MyClass::m_ClassMap. Does anybody know why ? I don't...

    To describe why I do the things explained above: Because of Win32-Architecture I have to build one function of my class as static to avoid global functions. Win32API wants a function-pointer and is not very amused about class-methods because of the implicit this-pointer they have.

    For that reason I decided to make one static function for handling all. This function gets the Id of the actual instance and looks up this id in the map described above, gets the associated instance-pointer and calls a function upon it.

    I think this is the easiest way to handle with this case, anyway I would appreciate any suggestions to get this done in a better way.

    But for now: Why do I get this Linkererror ?

    Thanx in advance

    Juergen

  2. #2
    Join Date
    Sep 2002
    Location
    Belarus - Tirol, Austria
    Posts
    647
    If u have a static member

    for ex. say:

    foo.h
    class foo
    {
    static int a;

    };

    U need to write somewhere in foo.cpp

    static foo::int a = 0;

    So U need to write in cpp-file

    static CMyClass::map<...>m_maplist;

    Got it?

    Last edited by dimm_coder; April 25th, 2003 at 06:50 AM.
    "UNIX is simple; it just takes a genius to understand its simplicity!"

  3. #3
    Join Date
    Aug 2002
    Location
    Hamburg / Germany
    Posts
    280

    i'm sorry, it doesn't work

    the compiler gives me a redeclaration-error when I do this

    I remember something like the syntax you used from the last time I had a nearly simmilar problem, but I can't remember (

  4. #4
    Join Date
    Sep 2002
    Location
    Belarus - Tirol, Austria
    Posts
    647
    Show both h and cpp files with a code related to your class and this problem.
    "UNIX is simple; it just takes a genius to understand its simplicity!"

  5. #5
    Join Date
    Jun 2001
    Location
    Switzerland
    Posts
    4,443
    This will compile (if you define inst):
    Code:
    class MyClass
    {
    public:
        MyClass()
        {
            m_ClassMap[inst]=this;
        }
    private:
        static std::map<int,MyClass*> m_ClassMap;
    };
    
    std::map<int,MyClass*> MyClass::m_ClassMap;
    Gabriel, CodeGuru moderator

    Forever trusting who we are
    And nothing else matters
    - Metallica

    Learn about the advantages of std::vector.

  6. #6
    Join Date
    Apr 1999
    Posts
    27,449

    Re: Problem with static class-element

    Originally posted by AlionSolutions
    The class looks like this:
    Code:
    class MyClass
    {
      private:
        map<int,MyClass*> m_ClassMap;
    
    
      public:
        MyClass()
    
      ..
      ..
    
    }
    Funny, I don't see any static members declared in your code. If you have an error, please post the exact code that is in your class. It just leads to confusion if you don't post your actual code.
    So far, so good. In the Constructor each of the instances shall register itself to the classmap. Therefore I put this inside the constructor:

    m_ClassMap[inst] = this;

    where inst is a value of type integer, for identifying a particular instance later.

    Ok,when I compile it I get a linker-error telling me, that there is an unresolved external MyClass::m_ClassMap. Does anybody know why ? I don't...
    With the code you posted, there should be no such error. If you meant that your class looks like this:
    Code:
    class MyClass
    {
      private:
        static map<int,MyClass*> m_ClassMap;
    
    };
    Then you must also define the static member. This is the rule of C++ and has nothing to do with map. Static members of classes must be defined in one of your modules:
    Code:
    // In one of your CPP files, and outside of any functions:
    map<int, MyClass*> MyClass::m_ClassMap;
    Also, you should use a typedef for your map. You'll go nuts typing in map<int, MyClass*> everywhere in your code.
    Code:
    class MyClass;
    typedef std::map<int, MyClass*> MyClassMap;
    
    class MyClass
    {
        private:
           static MyClassMap m_ClassMap;
    };
    Regards,

    Paul McKenzie

  7. #7
    Join Date
    Sep 2002
    Location
    Belarus - Tirol, Austria
    Posts
    647
    Originally posted by dimm_coder
    If u have a static member

    U need to write somewhere in foo.cpp
    static foo::int a = 0;
    So U need to write in cpp-file
    static CMyClass::map<...>m_maplist;
    Got it?

    Sorry, It was the fade of my mind //
    of course
    static int foo::a = 0;
    and
    static map<...> CMyClass::m_maplist;

    Sorry.
    "UNIX is simple; it just takes a genius to understand its simplicity!"

  8. #8
    Join Date
    Aug 2002
    Location
    Hamburg / Germany
    Posts
    280

    Thanx again Paul

    As many times I lost my brain, you helped me again to regain parts of it ;o)

    THNX

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured