Static Member

This is a discussion on Static Member within the C++ Programming forums, part of the General Programming Boards category; I have the following 2 files: Code: // File: DummyClass.h class DummyClass { public: void doNothing() { } }; // ...

  1. #1
    Registered User
    Join Date
    Jul 2005
    Posts
    15

    Static Member

    I have the following 2 files:
    Code:
    // File: DummyClass.h
    class DummyClass {
      public:
        void doNothing() {
        }
    };
    
    // File: MyMain.cpp
    #include "dummyclass.h"
    
    class MyMainClass {
      public:
        static DummyClass *dumbClass;
       
        MyMainClass(DummyClass dc) {
          dumbClass = dc;
        }
    };
    
    int main() {
      MyMainClass myClass(new DummyClass);
      myClass->dumbClass->doNothing();
      return 0;
    }
    It generates the following linking error:
    error LNK2001: unresolved external symbol "public: static class DummyClass * MyMainClass::dumbClass" (?dumbClass@Edge@@2PAVDummyClass@@A)

    If I remove static, it works just fine.

  2. #2
    Registered User
    Join Date
    Apr 2003
    Posts
    2,662
    If I remove static, it works just fine.
    I don't think that's possible. I see errors that have nothing to do with static.

  3. #3
    vae victus! skorman00's Avatar
    Join Date
    Nov 2003
    Posts
    594
    You have to initialize the static member somewhere else, having it in the constructor does not make C++ happy. The way you have your code set up, it would probably be best to have it right after your MyMainClass declaration (usually it's put in the .cpp for the corresponding class).
    Code:
    DummyClass * MyMainClass::dumbClass = /*???*/;

  4. #4
    Registered User
    Join Date
    Jul 2005
    Posts
    15
    There are some typos. See my correction below:

    Code:
    // File: DummyClass.h
    class DummyClass {
      public:
        void doNothing() {
        }
    };
    
    // File: MyMain.cpp
    #include "dummyclass.h"
    
    class MyMainClass {
      public:
        static DummyClass *dumbClass;
       
        MyMainClass(DummyClass *dc) {
          dumbClass = dc;
        }
    };
    
    int main() {
      MyMainClass myClass(new DummyClass);
      myClass.dumbClass->doNothing();
      return 0;
    }

  5. #5
    Registered User
    Join Date
    Jul 2005
    Posts
    15
    Quote Originally Posted by skorman00
    You have to initialize the static member somewhere else, having it in the constructor does not make C++ happy. The way you have your code set up, it would probably be best to have it right after your MyMainClass declaration (usually it's put in the .cpp for the corresponding class).
    Code:
    DummyClass * MyMainClass::dumbClass = /*???*/;
    Oh thanx Transformer for reminding me about static nuisance.

    I intend MyMainClass to have only a single (global) pointer to an instance of DummyClass. How do I code this?

  6. #6
    vae victus! skorman00's Avatar
    Join Date
    Nov 2003
    Posts
    594
    You've got it already since it's static. And since it's public, it's essentially global.

    Now you know, and knowing is half the battle!

  7. #7
    Registered User
    Join Date
    Jul 2005
    Posts
    15
    Sorry for not being clear. What I meant was, if I instantiated 2 objects of MyMainClass, each object would have its separate "dumbClass" pointer. But I wish them to share the same pointer (that was what I meant by global [I should have said singleton]).

  8. #8
    Registered User
    Join Date
    Jul 2005
    Posts
    15
    Oh! I understood your first message now. Thanx again.
    Last edited by dickyDick; 08-02-2005 at 04:17 PM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. const correctness and static member variables
    By cunnus88 in forum C++ Programming
    Replies: 2
    Last Post: 02-21-2008, 04:26 PM
  2. Linking errors with static var
    By Elysia in forum C++ Programming
    Replies: 8
    Last Post: 10-27-2007, 05:24 PM
  3. Replies: 28
    Last Post: 07-16-2006, 11:35 PM
  4. Static member functions more efficient?
    By drrngrvy in forum C++ Programming
    Replies: 6
    Last Post: 06-16-2006, 07:07 AM
  5. filling a static member map variable
    By elad in forum C++ Programming
    Replies: 2
    Last Post: 09-17-2004, 12:43 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21