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. an link error

an link error

Scheduled Pinned Locked Moved C / C++ / MFC
questionhelp
6 Posts 4 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.
  • R Offline
    R Offline
    richardye
    wrote on last edited by
    #1

    I write a program to count the number of objects. But when I compile it, I met with link errors. How can I resolve it? Head files: template class Counted { public: class TooManyObjects{}; static int getObjectNum() { return m_ObjectNum; }; protected: Counted(); Counted(const Counted& count); ~Counted() { --m_ObjectNum; }; private: static int m_ObjectNum; static const size_t m_MaxObjectNum; void init(); }; int Counted::m_ObjectNum = 0; const size_t Counted::m_MaxObjectNum = 5; class Printer: private Counted { public: static Printer* makePrinter(); static Printer* makePrinter(const Printer& rhs); ~Printer(); void reset(); void performSelfTest(); using Counted::getObjectNum; using Counted::TooManyObjects; private: Printer(); Printer(const Printer& rhs); }; in definition file: template Counted::Counted() { init(); } template Counted::Counted(const Counted& count) { init(); } template Counted::init() { if(m_ObjectNum > m_MaxObjectNum) { throw TooManyObjects(); } ++m_ObjectNum; } Printer::Printer(){} Printer::Printer(const Printer &rhs) {} Printer::~Printer() {} Printer* Printer::makePrinter() { return new Printer(); } Printer* Printer::makePrinter(const Printer& rhs) { return new Printer(rhs); } error LNK2001 private static int Counted::m_ObjectNum"XXX" is unresloved error LNK2019 protected: _thiscall Counted::Counted(void) "private _thiscall Printer::Printer(void)" why? Does the static memeber of a class can only be initialized by its base class. How about its derived class? Many thanks

    B CPalliniC 2 Replies Last reply
    0
    • R richardye

      I write a program to count the number of objects. But when I compile it, I met with link errors. How can I resolve it? Head files: template class Counted { public: class TooManyObjects{}; static int getObjectNum() { return m_ObjectNum; }; protected: Counted(); Counted(const Counted& count); ~Counted() { --m_ObjectNum; }; private: static int m_ObjectNum; static const size_t m_MaxObjectNum; void init(); }; int Counted::m_ObjectNum = 0; const size_t Counted::m_MaxObjectNum = 5; class Printer: private Counted { public: static Printer* makePrinter(); static Printer* makePrinter(const Printer& rhs); ~Printer(); void reset(); void performSelfTest(); using Counted::getObjectNum; using Counted::TooManyObjects; private: Printer(); Printer(const Printer& rhs); }; in definition file: template Counted::Counted() { init(); } template Counted::Counted(const Counted& count) { init(); } template Counted::init() { if(m_ObjectNum > m_MaxObjectNum) { throw TooManyObjects(); } ++m_ObjectNum; } Printer::Printer(){} Printer::Printer(const Printer &rhs) {} Printer::~Printer() {} Printer* Printer::makePrinter() { return new Printer(); } Printer* Printer::makePrinter(const Printer& rhs) { return new Printer(rhs); } error LNK2001 private static int Counted::m_ObjectNum"XXX" is unresloved error LNK2019 protected: _thiscall Counted::Counted(void) "private _thiscall Printer::Printer(void)" why? Does the static memeber of a class can only be initialized by its base class. How about its derived class? Many thanks

      B Offline
      B Offline
      baerten
      wrote on last edited by
      #2

      error LNK2001 private static int Counted::m_ObjectNum"XXX" is unresloved In the definition file, you need to declare it also. ( One time in the .h and one time in the .cpp) I hope it resolves this error

      CPalliniC 1 Reply Last reply
      0
      • R richardye

        I write a program to count the number of objects. But when I compile it, I met with link errors. How can I resolve it? Head files: template class Counted { public: class TooManyObjects{}; static int getObjectNum() { return m_ObjectNum; }; protected: Counted(); Counted(const Counted& count); ~Counted() { --m_ObjectNum; }; private: static int m_ObjectNum; static const size_t m_MaxObjectNum; void init(); }; int Counted::m_ObjectNum = 0; const size_t Counted::m_MaxObjectNum = 5; class Printer: private Counted { public: static Printer* makePrinter(); static Printer* makePrinter(const Printer& rhs); ~Printer(); void reset(); void performSelfTest(); using Counted::getObjectNum; using Counted::TooManyObjects; private: Printer(); Printer(const Printer& rhs); }; in definition file: template Counted::Counted() { init(); } template Counted::Counted(const Counted& count) { init(); } template Counted::init() { if(m_ObjectNum > m_MaxObjectNum) { throw TooManyObjects(); } ++m_ObjectNum; } Printer::Printer(){} Printer::Printer(const Printer &rhs) {} Printer::~Printer() {} Printer* Printer::makePrinter() { return new Printer(); } Printer* Printer::makePrinter(const Printer& rhs) { return new Printer(rhs); } error LNK2001 private static int Counted::m_ObjectNum"XXX" is unresloved error LNK2019 protected: _thiscall Counted::Counted(void) "private _thiscall Printer::Printer(void)" why? Does the static memeber of a class can only be initialized by its base class. How about its derived class? Many thanks

        CPalliniC Offline
        CPalliniC Offline
        CPallini
        wrote on last edited by
        #3

        your code seems bad formatted, use <pre> tags as follows <pre> //code here... </pre> to repost edit your post. :) -- modified at 5:30 Monday 19th November, 2007

        If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler. -- Alfonso the Wise, 13th Century King of Castile.

        In testa che avete, signor di Ceprano?

        T 1 Reply Last reply
        0
        • CPalliniC CPallini

          your code seems bad formatted, use <pre> tags as follows <pre> //code here... </pre> to repost edit your post. :) -- modified at 5:30 Monday 19th November, 2007

          If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler. -- Alfonso the Wise, 13th Century King of Castile.

          T Offline
          T Offline
          toxcct
          wrote on last edited by
          #4

          CPallini wrote:

          to repost

          to repost ? no ! to edit ! ;P


          [VisualCalc][Binary Guide][CommDialogs] | [Forums Guidelines]

          CPalliniC 1 Reply Last reply
          0
          • B baerten

            error LNK2001 private static int Counted::m_ObjectNum"XXX" is unresloved In the definition file, you need to declare it also. ( One time in the .h and one time in the .cpp) I hope it resolves this error

            CPalliniC Offline
            CPalliniC Offline
            CPallini
            wrote on last edited by
            #5

            baerten wrote:

            One time in the .h

            You don't need it. :)

            If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler. -- Alfonso the Wise, 13th Century King of Castile.

            In testa che avete, signor di Ceprano?

            1 Reply Last reply
            0
            • T toxcct

              CPallini wrote:

              to repost

              to repost ? no ! to edit ! ;P


              [VisualCalc][Binary Guide][CommDialogs] | [Forums Guidelines]

              CPalliniC Offline
              CPalliniC Offline
              CPallini
              wrote on last edited by
              #6

              toxcct wrote:

              to edit !

              Sure! Hence I've to edit my post too. :doh: :)

              If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler. -- Alfonso the Wise, 13th Century King of Castile.

              In testa che avete, signor di Ceprano?

              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