开发者

Boost 1.44.0 + VS2010 Private member error

I have a class declaration in Utils.h:

    class Utils {
 private:
     static boost::mutex outputMutex;
    };

In the cpp file:

boost::mutex Utils::outputMutex = boost::mutex();

I get:

Error 1 error C2248: 'boost::mutex::mutex' : cannot access private member declared in class 'boost::mutex'

If we look inside boost/thread/win32/mutex.hpp we see:

namespace boost
{   
    class mutex:
        public ::boost::detail::underlying_mutex
    {

    // ...       

    public:
        mutex()
        {
            initialize();
开发者_如何学运维        }

Does anyone know what I'm missing here? It used to compile OK on a different machine with VS2008.

Thank you.


What you have is copy-initialization, and is equivalent to:

boost::mutex Utils::outputMutex(boost::mutex());

Which calls the copy-constructor. However, mutex is noncopyable. Just let it default construct:

boost::mutex Utils::outputMutex;


The .cpp file should be:

boost::mutex Utils::outputMutex;

There's no need for an assignment. It will be constructed appropriately.


It looks like you're declaring Utils::outputMutex twice, once in the class declaration and then again in the .cpp. Also, the second declaration is being assigned the "return value" of the constructor, which isn't possible. What happens if you remove the second declaration?

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜