Link error when using static members

Discussion in 'C++' started by Andy, Sep 17, 2004.

  1. Andy

    Andy Guest

    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;
    }
    Andy, Sep 17, 2004
    #1
    1. Advertising

  2. Andy

    Sharad Kala Guest

    "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-lite/ctors.html#faq-10.10

    Sharad
    Sharad Kala, Sep 17, 2004
    #2
    1. Advertising

  3. Andy

    Andy Guest

    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-lite/ctors.html#faq-10.10
    >
    > Sharad
    >
    >
    Andy, Sep 17, 2004
    #3
  4. Andy

    Sharad Kala Guest

    "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
    Sharad Kala, Sep 17, 2004
    #4
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Ben
    Replies:
    3
    Views:
    416
  2. SaravanaKumar
    Replies:
    6
    Views:
    9,388
    Tony Morris
    Oct 19, 2004
  3. JFCM
    Replies:
    4
    Views:
    5,732
  4. Jean-Francois Brault

    can't link class with static methods and members

    Jean-Francois Brault, Apr 17, 2004, in forum: C++
    Replies:
    1
    Views:
    483
    Buster
    Apr 17, 2004
  5. Rakesh Sinha
    Replies:
    4
    Views:
    1,847
    Rakesh Sinha
    Jan 13, 2005
Loading...

Share This Page