an link error
-
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
-
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
-
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
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.
-
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.
CPallini wrote:
to repost
to repost ? no ! to edit ! ;P
[VisualCalc][Binary Guide][CommDialogs] | [Forums Guidelines]
-
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
-
CPallini wrote:
to repost
to repost ? no ! to edit ! ;P
[VisualCalc][Binary Guide][CommDialogs] | [Forums Guidelines]