-
October 28th, 2003, 12:13 PM
#1
CStringArray as static member variable
I get this linker error if I use a CStringArray as a static member ..
does any1 know why and how to do it right
error LNK2001: unresolved external symbol "protected: static class CStringArray CMyClass::m_staticStrArrayManualEntries" (?m_staticStrArrayManualEntries@CMyClass@@1VCStringArray@@A)
-
October 28th, 2003, 12:25 PM
#2
You need to declare the static member in the implementation file where you want to place it "physically":
Code:
// In your .h:
class CMyClass
{
...
protected:
static CStringArray m_staticStrArrayManualEntries;
}
// In your .cpp:
CStringArray CMyClass::m_staticStrArrayManualEntries;
-
October 28th, 2003, 12:41 PM
#3
Should this be at file scope in the .cpp file.
if I kept it inside a function then it would complain
error C2655: 'm_staticStrArrayManualEntries' : definition or redeclaration illegal in current scope
c:\development\myclass.h(46) : see declaration of 'm_staticStrArrayManualEntries'
Then I moved it to top of the .Cpp file like
Code:
#include "MainFrame.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
CStringArray CMyClass::m_staticStrArrayManualEntries;
and it links fine ..
How is this different from a global variable ... is this still a static member variable of the class ?
-
October 28th, 2003, 12:47 PM
#4
Originally posted by voidspace
Should this be at file scope in the .cpp file.
Yes.
How is this different from a global variable ... is this still a static member variable of the class ?
The difference it the CMyClass:: in front of m_staticStrArrayManualEntries. Actually, a static member is technically just a global variable. But the preceding CMyClass:: limits its visibility to members of CMyClass.
-
October 28th, 2003, 12:50 PM
#5
Great!.. Thats exactly the way I want it
Thank you
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|