Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. static member funcs/variables

static member funcs/variables

Scheduled Pinned Locked Moved C / C++ / MFC
c++helpquestion
5 Posts 3 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • M Offline
    M Offline
    moredip
    wrote on last edited by
    #1

    OK, I've got some code which I cut and paste straight from the MSDN documentation on the static C++ keyword.....and it won't link :mad: can anyone work out why? This is the code:

    // C++ only

    class SavingsAccount
    {
    public:
    static void setInterest( float newValue ) // Member function
    { currentRate = newValue; } // that accesses
    // only static
    // members
    private:
    char name[30];
    float total;
    static float currentRate; // One copy of this member is
    // shared among all instances
    // of SavingsAccount
    };

    // Static data members must be initialized at file scope, even
    // if private.
    float SavingsAccount::currentRate = 0.00154;

    I copied it into a header file, and I'm getting the following errors

    Tracker.obj : error LNK2005: "private: static float SavingsAccount::currentRate" (?currentRate@SavingsAccount@@0MA) already defined in Capture.obj
    Tracker.obj : error LNK2005: "protected: static int CTracker::a" (?a@CTracker@@1HA) already defined in Capture.obj
    Tracker.obj : warning LNK4006: "private: static float SavingsAccount::currentRate" (?currentRate@SavingsAccount@@0MA) already defined in Capture.obj; second definition ignored

    By the way, this isn't just me nitpicking at MSDN, I have a very similar class that throws up the same errors, and I need to know why! TIA, Pete

    P 1 Reply Last reply
    0
    • M moredip

      OK, I've got some code which I cut and paste straight from the MSDN documentation on the static C++ keyword.....and it won't link :mad: can anyone work out why? This is the code:

      // C++ only

      class SavingsAccount
      {
      public:
      static void setInterest( float newValue ) // Member function
      { currentRate = newValue; } // that accesses
      // only static
      // members
      private:
      char name[30];
      float total;
      static float currentRate; // One copy of this member is
      // shared among all instances
      // of SavingsAccount
      };

      // Static data members must be initialized at file scope, even
      // if private.
      float SavingsAccount::currentRate = 0.00154;

      I copied it into a header file, and I'm getting the following errors

      Tracker.obj : error LNK2005: "private: static float SavingsAccount::currentRate" (?currentRate@SavingsAccount@@0MA) already defined in Capture.obj
      Tracker.obj : error LNK2005: "protected: static int CTracker::a" (?a@CTracker@@1HA) already defined in Capture.obj
      Tracker.obj : warning LNK4006: "private: static float SavingsAccount::currentRate" (?currentRate@SavingsAccount@@0MA) already defined in Capture.obj; second definition ignored

      By the way, this isn't just me nitpicking at MSDN, I have a very similar class that throws up the same errors, and I need to know why! TIA, Pete

      P Offline
      P Offline
      Paul M Watt
      wrote on last edited by
      #2

      this line:

      // Static data members must be initialized at file scope, even
      // if private.
      float SavingsAccount::currentRate = 0.00154;

      Needs to be placed into your .cpp file. Currently it is in your header file. When it gets included into multiple files the linker tries to allocate multiple instances of the static variable, and that is why you are seeing that probrlm.

      M 1 Reply Last reply
      0
      • P Paul M Watt

        this line:

        // Static data members must be initialized at file scope, even
        // if private.
        float SavingsAccount::currentRate = 0.00154;

        Needs to be placed into your .cpp file. Currently it is in your header file. When it gets included into multiple files the linker tries to allocate multiple instances of the static variable, and that is why you are seeing that probrlm.

        M Offline
        M Offline
        moredip
        wrote on last edited by
        #3

        Thanks! Ok, so if you have a static member variable then it has to be provided with a global definition, which is what that line does, right? But what if the variable is a user-defined type? I'm getting LNK2001 errors for all of the static variables of user-defined types in my class. I'm guessing it's because VC++ doesn't know how to initialize them correctly. So here's what I've tried so far - The variables are declared in my header file as follows:

        class CTracker
        {
        ...
        protected:
        ...
        static C2DPoint **m_aCentroids;
        static double ***m_aCentroidDirs;
        static UINT *m_aNumPoints;
        ...
        };

        I then initialize them in my cpp file like this:

        static C2DPoint CTracker::**m_aCentroids = NULL;
        static double CTracker::***m_aCentroidDirs = NULL;
        static UINT CTracker::*m_aNumPoints = NULL;

        but i'm still getting errors like the following:

        error LNK2001: unresolved external symbol "protected: static class C2DPoint * * CTracker::m_aCentroids" (?m_aCentroids@CTracker@@1PAPAVC2DPoint@@A)

        Any idea why? Oh, and thanks again for your help so far (with this /and/ the function pointer thing!). Pete

        D 1 Reply Last reply
        0
        • M moredip

          Thanks! Ok, so if you have a static member variable then it has to be provided with a global definition, which is what that line does, right? But what if the variable is a user-defined type? I'm getting LNK2001 errors for all of the static variables of user-defined types in my class. I'm guessing it's because VC++ doesn't know how to initialize them correctly. So here's what I've tried so far - The variables are declared in my header file as follows:

          class CTracker
          {
          ...
          protected:
          ...
          static C2DPoint **m_aCentroids;
          static double ***m_aCentroidDirs;
          static UINT *m_aNumPoints;
          ...
          };

          I then initialize them in my cpp file like this:

          static C2DPoint CTracker::**m_aCentroids = NULL;
          static double CTracker::***m_aCentroidDirs = NULL;
          static UINT CTracker::*m_aNumPoints = NULL;

          but i'm still getting errors like the following:

          error LNK2001: unresolved external symbol "protected: static class C2DPoint * * CTracker::m_aCentroids" (?m_aCentroids@CTracker@@1PAPAVC2DPoint@@A)

          Any idea why? Oh, and thanks again for your help so far (with this /and/ the function pointer thing!). Pete

          D Offline
          D Offline
          Derek Waters
          wrote on last edited by
          #4

          In your CPP file, you don't need the static on the front, and I think you've got the *s in the wrong place:

          C2DPoint **CTracker::m_aCentroids = NULL;
          double ***CTracker::m_aCentroidDirs = NULL;
          UINT *CTracker::m_aNumPoints = NULL;

          Hope this helps.


          Derek Waters
          derek@lj-oz.com

          M 1 Reply Last reply
          0
          • D Derek Waters

            In your CPP file, you don't need the static on the front, and I think you've got the *s in the wrong place:

            C2DPoint **CTracker::m_aCentroids = NULL;
            double ***CTracker::m_aCentroidDirs = NULL;
            UINT *CTracker::m_aNumPoints = NULL;

            Hope this helps.


            Derek Waters
            derek@lj-oz.com

            M Offline
            M Offline
            moredip
            wrote on last edited by
            #5

            Yep, that hit the spot! Thanks a lot Derek.

            1 Reply Last reply
            0
            Reply
            • Reply as topic
            Log in to reply
            • Oldest to Newest
            • Newest to Oldest
            • Most Votes


            • Login

            • Don't have an account? Register

            • Login or register to search.
            • First post
              Last post
            0
            • Categories
            • Recent
            • Tags
            • Popular
            • World
            • Users
            • Groups