Archived

This topic is now archived and is closed to further replies.

i_sys

If anyone can make this small code snipit Link (compiles fine) I'd be grateful)

Recommended Posts

i_sys    122
Hi, I''m trying to get a static member function in my class to access the other non-static members. I know thats a no-no but I`ve seen it done. Only thing is my version doesnt work and I`ve lost access to the code that showed how its done. Anyhow, I think I`m close because this compiles) It generates a Link error though. Heres the test I`m using to figure it out. If anyone can correct this code and your a very clever gynacologist too I`ll have your babies . Thanks in advance. My classes Header file: class cTest { protected: int iVar; public: int iFunc() { return iVar; } static int iSFunc(); static cTest *m_pTest; public: cTest(): iVar(10) { m_pTest = this; } }; inline cTest *GetTest() { return cTest::m_pTest; } My classes Body file: int cTest::iSFunc() { return GetTest()->iFunc(); } Generated Link2001 error: cGameEngine.obj : error LNK2001: unresolved external symbol "public: static class cTest * cTest::m_pTest" (?m_pTest@cTest@@2PAV1@A) cGameEngine is just the point in my code where I instantiate an instance of cTest)

Share this post


Link to post
Share on other sites
Dactylos    122
Perhaps you should define your static members in a cpp file (only in one).

ex:
    
cTest* cTest::m_pTest = NULL;

and similarly for all your static members.

If you don't do this the compiler won't know in which module to reserve space for the data.

Edited by - Dactylos on July 31, 2001 7:51:52 AM

Share this post


Link to post
Share on other sites
i_sys    122
Hurrah! Thanks that works a treat. Finally I can encapsulate WndProc without losing any important functionality, and have my own complete cWindow class. Thanks I owe you)

Edited by - i_sys on July 31, 2001 7:56:48 AM

Share this post


Link to post
Share on other sites