473,387 Members | 1,606 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

Link error when using static members

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;
}
Jul 22 '05 #1
3 1758

"Andy" <do**@spam.me> 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
Jul 22 '05 #2
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" <no******************@yahoo.com> wrote in message
news:2q*************@uni-berlin.de...

"Andy" <do**@spam.me> 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

Jul 22 '05 #3

"Andy" <do**@spam.me> wrote in message news:2q*************@uni-berlin.de...
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
Jul 22 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
by: sriram | last post by:
I have the following: class BSA { ... ... ... ... public: enum VRGAttrId {
4
by: Scott | last post by:
We are trying to build a managed C++ static link library containing several classes which are members of a namespace. The build of this library seems to work OK. No errors. We then try to use...
6
by: Bill Rubin | last post by:
The following code snippet shows that VC++ 7.1 correctly compiles a static member function invocation from an Unrelated class, since this static member function is public. I expected to compile the...
1
by: Paulustrious | last post by:
Newbie using VC# Express..... Part the First ======== Can some one tell me why Example 1 compiles and Example 2 does not... Example 1 ------------ using System;
12
by: mast2as | last post by:
Hi everyone... I have a TExceptionHandler class that is uses in the code to thow exceptions. Whenever an exception is thrown the TExceptionHander constructor takes an error code (int) as an...
7
by: The|Godfather | last post by:
Hi everybody, I read Scotte Meyer's "Effective C++" book twice and I know that he mentioned something specific about constructors and destructors that was related to the following...
3
by: NewToCPP | last post by:
typedef int (*ActionFunction) (); class FunctionInterface { static ActionFunction actionFn; public: static void init(); static void initActionFunctions(); static action_process1();
3
by: Baby Lion | last post by:
hi,everyone , one Runtime error occur when running , maybe it's about assigning memory . I need your help , thanks. It's about the travelling sellman problem . //#include "stdafx.h" ...
12
by: prashant | last post by:
hi, i am trying to create an xml tag ref to hold link.php?id=1; or link.php?id=2 and so on. I want the links for all the fields(id) in the databse so that when i call them in my html page for...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.