Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > Link error when using static members

Reply
Thread Tools

Link error when using static members

 
 
Andy
Guest
Posts: n/a
 
      09-17-2004
I'm trying to make a Config-class that has only one instance, as the code
below states. However, when I try to use the instance with
CConfig::GetInstance() in another .cpp-file I get a link error:

StaticTest error LNK2001: unresolved external symbol "private: static class
CConfig CConfig::m_instance" (?m_instance@CConfig@@0V1@A)

Why is this?

//Config.h
class CConfig
{
private:
static CConfig m_instance;
public:
static CConfig& GetInstance();
CConfig();
~CConfig();
};

/Config.cpp
include "StdAfx.h"
#include ".\config.h"
CConfig::CConfig()
{
}
CConfig::~CConfig()
{
}
CConfig& CConfig::GetInstance()
{
return m_instance;
}


 
Reply With Quote
 
 
 
 
Sharad Kala
Guest
Posts: n/a
 
      09-17-2004

"Andy" <> wrote in message
> I'm trying to make a Config-class that has only one instance, as the code
> below states. However, when I try to use the instance with
> CConfig::GetInstance() in another .cpp-file I get a link error:
>
> StaticTest error LNK2001: unresolved external symbol "private: static

class
> CConfig CConfig::m_instance" (?m_instance@CConfig@@0V1@A)


Check this FAQ - http://www.parashift.com/c++-faq-lit...html#faq-10.10

Sharad


 
Reply With Quote
 
 
 
 
Andy
Guest
Posts: n/a
 
      09-17-2004
I had a look at that example, but I'm sorry to say I didn't understand it
fully. The code later in this message works fine if I write

CConfig CConfig::m_instance = CConfig(); // 1

in any cpp-file, but not if write

CString CConfig::GetString() = "test"; // 2

As I'm starting to panic here, would you please state exactly what to with
my example below to make it work to use CConfig::GetString(); Do I really
have to use // 1 above?

class CConfig
{
private:
static CConfig m_instance;
CString m_string;
static CConfig& GetInstance();
public:
static CString GetString()
{
return m_instance.m_string;
};
CConfig();
~CConfig();
};

/Config.cpp

#include ".\config.h"
CConfig::CConfig()
{
m_string = "Test";
}
CConfig::~CConfig()
{
}

CConfig& CConfig::GetInstance()
{
return m_instance;
}


"Sharad Kala" <> wrote in message
news:...
>
> "Andy" <> wrote in message
> > I'm trying to make a Config-class that has only one instance, as the

code
> > below states. However, when I try to use the instance with
> > CConfig::GetInstance() in another .cpp-file I get a link error:
> >
> > StaticTest error LNK2001: unresolved external symbol "private: static

> class
> > CConfig CConfig::m_instance" (?m_instance@CConfig@@0V1@A)

>
> Check this FAQ -

http://www.parashift.com/c++-faq-lit...html#faq-10.10
>
> Sharad
>
>



 
Reply With Quote
 
Sharad Kala
Guest
Posts: n/a
 
      09-17-2004

"Andy" <> wrote in message news:...
> I had a look at that example, but I'm sorry to say I didn't understand it
> fully. The code later in this message works fine if I write


ok, the mistake was that you had done in your original code was that you
just declared the static member CConfig::m_instance without defining it.
Remember static members belong to the *class* and not objects (i.e. they are
shared amongst objects of that class). You have to explicitly define it else
linker will crib (Note - There are some relaxations for static const ints
and enums, you can initialize them in the class body without explicit
definition *if* they are not used in the program)

>
> CConfig CConfig::m_instance = CConfig(); // 1


This makes the program work becuase you are providing a definition for the
static member. This is correct.

>
> in any cpp-file, but not if write
>
> CString CConfig::GetString() = "test"; // 2


CConfig::GetString() = "test";
For this call to work CConfig::GetString() should return a reference.
This is completely unrelated ti your original problem. Line 1 should fix
your problem.

Sharad


 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Thread safety problems with function scope static variables vs class static private members Hicham Mouline C++ 5 12-19-2008 08:10 PM
Difference between static final members and final static members(if any)? JFCM Java 4 02-07-2006 11:32 AM
About static const members appearing in another static const definitions Rakesh Sinha C++ 4 01-13-2005 08:11 AM
Instantiating a static class( Class with all static members - methods and variables) SaravanaKumar Java 6 10-19-2004 08:20 AM
Static classes with static members Ben ASP .Net 3 06-01-2004 07:43 PM



Advertisments