简体   繁体   English

无法访问singleton类析构函数中的私有成员

[英]Cannot access private member in singleton class destructor

I'm trying to implement this singleton class. 我正在尝试实现这个单例类。 But I encountered this error: 但是我遇到了这个错误:

'Singleton::~Singleton': cannot access private member declared in class 'Singleton' This is flagged in the header file, the last line which contains the closing brace. 'Singleton :: ~Singleton':无法访问类'Singleton'中声明的私有成员。这在头文件中标记,最后一行包含右括号。

Can somebody help me explain what is causing this problem? 有人可以帮我解释导致这个问题的原因吗? Below is my source code. 以下是我的源代码。

Singleton.h: Singleton.h:


class Singleton
{
public:
    static Singleton* Instance()
    {
        if( !pInstance )
        {
            if( destroyed )
            {
                // throw exception
            }
            else
            {
                Create();
            }

        }
        return pInstance;
    }
private:
    static void Create()
    {
        static Singleton myInstance;
        pInstance = &myInstance;
    }
    Singleton() {}
    Singleton( const Singleton& );
    Singleton& operator=( const Singleton& );
    ~Singleton() 
    {
        pInstance = 0;
        detroyed = false;
    }

    static Singleton* pInstance;
    static bool destroyed;
};

Singleton.cpp: Singleton.cpp:


Singleton* Singleton::pInstance = 0;
bool Singleton::destroyed = false;

Inside my main function: 在我的主要功能内:


Singleton* s = Singleton::Instance();

If I make the destructor as public, then the problem disappears. 如果我将析构函数设为公共,那么问题就会消失。 But a book (Modern C++ Design) says it should be private to prevent users from deleting the instance. 但是一本书(Modern C ++ Design)说它应该是私有的,以防止用户删除该实例。 I actually need to put some code for cleanup for pInstance and destroyed inside the destructor. 我实际上需要为pInstance放置一些清理代码并在析构函数内部销毁。

By the way, I'm using Visual C++ 6.0 to compile. 顺便说一下,我正在使用Visual C ++ 6.0进行编译。

The code you posted looks doesn't have any problem, so the problem must be in some other part of your source code. 您发布的代码看起来没有任何问题,因此问题必须出在源代码的其他部分。

The error message will be preceded by the filename and line number where the problem is occurring. 错误消息前面将出现问题所在的文件名和行号。 Please look at the line and you'll see a bit of code that is either trying to call delete on a singleton pointer or is trying to construct an instance of singleton. 请查看该行,您将看到一些代码尝试在单例指针上调用delete或尝试构造单例实例。

The error message will look something like this (the file and line number are just an example): 错误消息看起来像这样(文件和行号只是一个例子):

c:\path\to\file.cpp(41) : error C2248: 'Singleton::~Singleton': cannot access private member declared in class 'Singleton'

So in that case, you would want to see what is happening at line 41 in file.cpp. 因此,在这种情况下,您可能希望查看file.cpp中第41行发生的情况。

You should probably have let us know that the version of Visual C++ you're working with is VC6. 您可能应该告诉我们您正在使用的Visual C ++版本是VC6。 I can repro the error with that. 我可以用那个重复错误。

At this point, I have no suggestion other than to move up to a newer version of MSVC if possible (VC 2008 is available at no cost in the Express edition). 此时,除了尽可能升级到更新版本的MSVC之外,我没有任何建议(在Express版本中免费提供VC 2008)。

Just a couple other data points - VC2003 and later have no problem with the Singleton destructor being private as in your sample. 只有几个其他数据点 - VC2003及更高版本没有问题, Singleton析构函数是私有的,就像你的样本一样。

我不是C ++或VC专家,但是您的示例看起来与此页面上描述的类似...作者称之为编译器错误。

Personally i haven't put destructors in my singletons unless i am using a template singleton class, but then i make them protected. 我个人没有把析构函数放在我的单身人士中,除非我使用模板单例类,但后来我让它们受到保护。

template<class T>
class Singleton
{
public:
    static T &GetInstance( void )
    {
        static T obj;
        return obj;
    }

    static T *GetInstancePtr( void )
    {
        return &(GetInstance());
    }

protected:
    virtual ~Singleton(){};
    Singleton(){};

};

then write my class as 然后把我的课写成

class LogWriter : public Singleton<LogWriter>
{
friend class Singleton<LogWriter>;
}
class Singleton
{
     static Singleton *pInstance = NULL;  
     Singleton(){};

     public:

    static Singleton * GetInstance() {

          if(!pInstance)
          {
               pInstance = new Singleton();
          }
          return pInstance;
     }

     static void  RemoveInstance() {

          if(pInstance)
          {
               delete pInstance;
               pInstance = NULL;
          }

     }
};

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM