關於c++編譯報隱式刪除的錯誤


1.這個問題可能是父類對於某種方法設置了=delete 或者是private

2.某些拷貝構造函數報這種錯誤,可能是因為某些成員變量自身不支持拷貝導致的

I have class looks as follows in .h file (Header)

#include <boost/thread.hpp>

class MyClass{

    private:
        boost::mutex bPoolMtx_;

        // ... other vars
    public:
        // public vars and methods

}

I get the following error trying to build/ compile.

MyClass.h:38:7: note: ‘MyClass::MyClass(const MyClass&)’ is implicitly deleted because the default definition would be ill-formed:
MyClass.h:38:7: error: use of deleted function ‘boost::mutex::mutex(const boost::mutex&)’

I don't use the mutex at all in the cpp file yet. When I comment out the boost::mutex line it builds fine. What is going on?

 

1 Answer

5
 

The default copy constructor generated by the compiler copies all data members by default. Your use of boost::mutex throws an error because the mutex isn't copyable.

You can write your own copy constructor that doesn't attempt to copy the mutex or simply delete the copy constructor for MyClass.

#include <boost/thread.hpp>

class MyClass{
    private:
        boost::mutex bPoolMtx_;

        // ... other vars
    public:
        // public vars and methods
        MyClass(const MyClass&) = delete;
}


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM