?
Solved

static variable - unresolved external symbol

Posted on 2009-04-14
11
Medium Priority
?
532 Views
Last Modified: 2013-11-20
Hi Experts,

I've got a class where I'm creating a static variable which is a pointer.  In the implementation file, in the cpp file, I reference this pointer and I get the unresolved external symbol...  What am I doing wrong?  I'm trying to implement a singleton ...   The code below is what I believe is just the essentials.....

Thanks,
Mike
///  this is the .h file.....
#include "afxdb.h"
class CMyClass
{
public:
           CMyClass();
          static CDatabase * m_pDatabase;   ////  if I comment out this line, no more error.......
}
 
///// cpp file:
CMyClass::CMyClass() {}
 
BOOL CMyClass::Connect()
{
        m_pDatabase = new CDatabase();
        return TRUE;
}

Open in new window

0
Comment
Question by:thready
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 6
  • 5
11 Comments
 
LVL 40

Accepted Solution

by:
mrjoltcola earned 2000 total points
ID: 24143722
A static member variable is essentially a global variable with class scope. So declaring one is not the same as defining one.

So add this to your .cpp file, it will define the single memory variable that is a static member variable.



///// cpp file:
 
// define static members
static CDatabase * CMyClass::m_pDatabase;
 
 
CMyClass::CMyClass() {}
 
BOOL CMyClass::Connect()
{
        m_pDatabase = new CDatabase();
        return TRUE;
}

Open in new window

0
 
LVL 40

Expert Comment

by:mrjoltcola
ID: 24143751
All that said, this is a throwback to C compatibility. Newer languages implement it in the compiler, but C++ is compatible with C linkers which expect the variable to exist in one compilation unit.

Maybe we see this ugly feature go away in C++0x
0
 
LVL 1

Author Closing Comment

by:thready
ID: 31570213
I keep coming back to this solution - I know about it - then I forget it......  Then I do the same thing again...  This time I hope I remember it....  It's from my java background.....  works in java - makes me hate c++....  But you gotta hate java for how slow it is....  Thanks for the response!
0
Get MongoDB database support online, now!

At Percona’s web store you can order your MongoDB database support needs in minutes. No hassles, no fuss, just pick and click. Pay online with a credit card. Handle your MongoDB database support now!

 
LVL 1

Author Comment

by:thready
ID: 24143928
Hi mrjoltcola - i`m getting this error - I did as you said - I got this error - I then removed the declaration I made in the .h - I still get this error......  How come?

C2761: 'CMyDatabase *CMyClass::m_pDatabase' : member function redeclaration not allowed
0
 
LVL 40

Expert Comment

by:mrjoltcola
ID: 24144000
Something changed, it thinks you are declaring a member function. Post your code again so I can see.
0
 
LVL 1

Author Comment

by:thready
ID: 24144020
It`s just that it thinks that m_pDatabase is a member function...  because of the :: operator perhaps?
0
 
LVL 1

Author Comment

by:thready
ID: 24144061
I apologize - this is the error I am getting:

error C2720: 'CMyClass::m_pDatabase' : 'static ' storage-class specifier illegal on members
0
 
LVL 1

Author Comment

by:thready
ID: 24144104
//  .h file:
class CDatabase;

class CMyClass
{
public:
     CMyClass();
     static CDatabase * m_pDatabase;
}

// .cpp file:
#include "afxdb.h"
static CDatabase * CMyClass::m_pDatabase;

///  with no other reference to m_pDatabase.....  (error c2720)
0
 
LVL 40

Expert Comment

by:mrjoltcola
ID: 24144112
You don't use the "static" keyword in the .cpp file, only in the .h file inside the class declaration. Another C throwback.
0
 
LVL 1

Author Comment

by:thready
ID: 24144125
Compiles - thanks again mrjoltcola - and I like the name - sounds heavily caffeinated...
0
 
LVL 40

Expert Comment

by:mrjoltcola
ID: 24144196
Only sometimes!
0

Featured Post

On Demand Webinar: Networking for the Cloud Era

Ready to improve network connectivity? Watch this webinar to learn how SD-WANs and a one-click instant connect tool can boost provisions, deployment, and management of your cloud connection.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Templates For Beginners Or How To Encourage The Compiler To Work For You Introduction This tutorial is targeted at the reader who is, perhaps, familiar with the basics of C++ but would prefer a little slower introduction to the more ad…
Introduction: Hints for the grid button.  Nested classes, templated collections.  Squash that darned bug! Continuing from the sixth article about sudoku.   Open the project in visual studio. First we will finish with the SUD_SETVALUE messa…
The goal of the tutorial is to teach the user how to use functions in C++. The video will cover how to define functions, how to call functions and how to create functions prototypes. Microsoft Visual C++ 2010 Express will be used as a text editor an…
The goal of the video will be to teach the user the concept of local variables and scope. An example of a locally defined variable will be given as well as an explanation of what scope is in C++. The local variable and concept of scope will be relat…
Suggested Courses

770 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question