Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
561 views
in Technique[技术] by (71.8m points)

dll - Visual Studio C++ reference deleted function compile error

If I try to compile this (Visual Studio 2015):

#define EXPORT_API __declspec(dllexport)

//#include "readerwriterqueue.h"
#include <map>
#include <thread>

class BaseClass
{
public:
    class A {
        std::thread thd;
        //moodycamel::BlockingReaderWriterQueue<int> processqueue;
    };

    std::map<unsigned int, A> mpmap;
};  // End class BaseClass

class EXPORT_API B : public BaseClass { };

I get an error message about

Severity    Code    Description Project File    Line    Suppression State
Error   C2280   'std::pair<const _Kty,_Ty>::pair(const std::pair<const _Kty,_Ty> &)': attempting to reference a deleted function    Example C:Program Files (x86)Microsoft Visual Studio 14.0VCincludexmemory0 737 

Console Output:

    Build started...
1>------ Build started: Project: Example, Configuration: Debug x64 ------
1>Example.cpp
1>C:Program Files (x86)Microsoft Visual Studio 14.0VCincludexmemory0(737): error C2280: 'std::pair<const _Kty,_Ty>::pair(const std::pair<const _Kty,_Ty> &)': attempting to reference a deleted function
1>        with
1>        [
1>            _Kty=unsigned int,
1>            _Ty=BaseClass::A
1>        ]
1>C:Program Files (x86)Microsoft Visual Studio 14.0VCincludeutility(112): note: see declaration of 'std::pair<const _Kty,_Ty>::pair'
1>        with
1>        [
1>            _Kty=unsigned int,
1>            _Ty=BaseClass::A
1>        ]
1>C:Program Files (x86)Microsoft Visual Studio 14.0VCincludexmemory0(857): note: see reference to function template instantiation 'void std::allocator<_Other>::construct<_Objty,_Ty&>(_Objty *,_Ty &)' being compiled
1>        with
1>        [
1>            _Other=std::_Tree_node<std::pair<const unsigned int,BaseClass::A>,void *>,
1>            _Objty=std::pair<const unsigned int,BaseClass::A>,
1>            _Ty=std::pair<const unsigned int,BaseClass::A>
1>        ]
1>C:Program Files (x86)Microsoft Visual Studio 14.0VCincludexmemory0(857): note: see reference to function template instantiation 'void std::allocator<_Other>::construct<_Objty,_Ty&>(_Objty *,_Ty &)' being compiled
1>        with
1>        [
1>            _Other=std::_Tree_node<std::pair<const unsigned int,BaseClass::A>,void *>,
1>            _Objty=std::pair<const unsigned int,BaseClass::A>,
1>            _Ty=std::pair<const unsigned int,BaseClass::A>
1>        ]
1>C:Program Files (x86)Microsoft Visual Studio 14.0VCincludexmemory0(996): note: see reference to function template instantiation 'void std::allocator_traits<_Alloc>::construct<_Ty,_Ty&>(std::allocator<_Other> &,_Objty *,_Ty &)' being compiled
1>        with
1>        [
1>            _Alloc=std::allocator<std::_Tree_node<std::pair<const unsigned int,BaseClass::A>,void *>>,
1>            _Ty=std::pair<const unsigned int,BaseClass::A>,
1>            _Other=std::_Tree_node<std::pair<const unsigned int,BaseClass::A>,void *>,
1>            _Objty=std::pair<const unsigned int,BaseClass::A>
1>        ]
1>C:Program Files (x86)Microsoft Visual Studio 14.0VCincludexmemory0(995): note: see reference to function template instantiation 'void std::allocator_traits<_Alloc>::construct<_Ty,_Ty&>(std::allocator<_Other> &,_Objty *,_Ty &)' being compiled
1>        with
1>        [
1>            _Alloc=std::allocator<std::_Tree_node<std::pair<const unsigned int,BaseClass::A>,void *>>,
1>            _Ty=std::pair<const unsigned int,BaseClass::A>,
1>            _Other=std::_Tree_node<std::pair<const unsigned int,BaseClass::A>,void *>,
1>            _Objty=std::pair<const unsigned int,BaseClass::A>
1>        ]
1>C:Program Files (x86)Microsoft Visual Studio 14.0VCincludextree(889): note: see reference to function template instantiation 'void std::_Wrap_alloc<std::allocator<_Other>>::construct<_Ty,_Ty&>(_Ty *,_Ty &)' being compiled
1>        with
1>        [
1>            _Other=std::_Tree_node<std::pair<const unsigned int,BaseClass::A>,void *>,
1>            _Ty=std::pair<const unsigned int,BaseClass::A>
1>        ]
1>C:Program Files (x86)Microsoft Visual Studio 14.0VCincludextree(887): note: see reference to function template instantiation 'void std::_Wrap_alloc<std::allocator<_Other>>::construct<_Ty,_Ty&>(_Ty *,_Ty &)' being compiled
1>        with
1>        [
1>            _Other=std::_Tree_node<std::pair<const unsigned int,BaseClass::A>,void *>,
1>            _Ty=std::pair<const unsigned int,BaseClass::A>
1>        ]
1>C:Program Files (x86)Microsoft Visual Studio 14.0VCincludextree(1939): note: see reference to function template instantiation 'std::_Tree_node<std::pair<const _Kty,_Ty>,void *> *std::_Tree_comp_alloc<_Traits>::_Buynode<std::pair<const _Kty,_Ty>&>(std::pair<const _Kty,_Ty> &)' being compiled
1>        with
1>        [
1>            _Kty=unsigned int,
1>            _Ty=BaseClass::A,
1>            _Traits=std::_Tmap_traits<unsigned int,BaseClass::A,std::less<unsigned int>,std::allocator<std::pair<const unsigned int,BaseClass::A>>,false>
1>        ]
1>C:Program Files (x86)Microsoft Visual Studio 14.0VCincludextree(1939): note: see reference to function template instantiation 'std::_Tree_node<std::pair<const _Kty,_Ty>,void *> *std::_Tree_comp_alloc<_Traits>::_Buynode<std::pair<const _Kty,_Ty>&>(std::pair<const _Kty,_Ty> &)' being compiled
1>        with
1>        [
1>            _Kty=unsigned int,
1>            _Ty=BaseClass::A,
1>            _Traits=std::_Tmap_traits<unsigned int,BaseClass::A,std::less<unsigned int>,std::allocator<std::pair<const unsigned int,BaseClass::A>>,false>
1>        ]
1>C:Program Files (x86)Microsoft Visual Studio 14.0VCincludextree(1966): note: see reference to function template instantiation 'std::_Tree_node<std::pair<const _Kty,_Ty>,void *> *std::_Tree<std::_Tmap_traits<_Kty,_Ty,_Pr,_Alloc,false>>::_Copy_or_move<std::pair<const _Kty,_Ty>,std::integral_constant<bool,false>>(std::pair<const _Kty,_Ty> &,std::_Tree<std::_Tmap_traits<_Kty,_Ty,_Pr,_Alloc,false>>::_Copy_tag,_Is_set)' being compiled
1>        with
1>        [
1>            _Kty=unsigned int,
1>            _Ty=BaseClass::A,
1>            _Pr=std::less<unsigned int>,
1>            _Alloc=std::allocator<std::pair<const unsigned int,BaseClass::A>>,
1>            _Is_set=std::integral_constant<bool,false>
1>        ]
1>C:Program Files (x86)Microsoft Visual Studio 14.0VCincludextree(1965): note: see reference to function template instantiation 'std::_Tree_node<std::pair<const _Kty,_Ty>,void *> *std::_Tree<std::_Tmap_traits<_Kty,_Ty,_Pr,_Alloc,false>>::_Copy_or_move<std::pair<const _Kty,_Ty>,std::integral_constant<bool,false>>(std::pair<const _Kty,_Ty> &,std::_Tree<std::_Tmap_traits<_Kty,_Ty,_Pr,_Alloc,false>>::_Copy_tag,_Is_set)' being compiled
1>        with
1>        [
1>            _Kty=unsigned int,
1>            _Ty=BaseClass::A,
1>            _Pr=std::less<unsigned int>,
1>            _Alloc=std::allocator<std::pair<const unsigned int,BaseClass::A>>,
1>            _Is_set=std::integral_constant<bool,false>
1>        ]
1>C:Program Files (x86)Microsoft Visual Studio 14.0VCincludextree(1921): note: see reference to function template instantiation 'std::_Tree_node<std::pair<const _Kty,_Ty>,void *> *std::_Tree<std::_Tmap_traits<_Kty,_Ty,_Pr,_Alloc,false>>::_Copy_nodes<_Moveit>(std::_Tree_node<std::pair<const _Kty,_Ty>,void *> *,std::_Tree_node<std::pair<const _Kty,_Ty>,void *> *,_Moveit)' being compiled
1>        with
1>        [
1>            _Kty=unsigned int,
1>            _Ty=BaseClass::A,
1>            _Pr=std::less<unsigned int>,
1>            _Alloc=std::allocator<std::pair<const unsigned int,BaseClass::A>>,
1>            _Moveit=std::_Tree<std::_Tmap_traits<unsigned int,BaseClass::A,std::less<unsigned int>,std::allocator<std::pair<const unsigned int,BaseClass::A>>,false>>::_Copy_tag
1>        ]
1>C:Program Files (x86)Microsoft Visual Studio 14.0VCincludextree(1921): note: see reference to function template instantiation 'std::_Tree_node<std::pair<const _Kty,_Ty>,void *> *std::_Tree<std::_Tmap_traits<_Kty,_Ty,_Pr,_Alloc,false>>::_Copy_nodes<_Moveit>(std::_Tree_node<std::pair<const _Kty,_Ty>,void *> *,std::_Tree_node<std::pair<const _Kty,_Ty>,void *> *,_Moveit)' being compiled
1>        with
1>        [
1>            _Kty=unsigned int,
1>            _Ty=BaseClass::A,
1>            _Pr=std::less<unsigned int>,
1>            _Alloc=std::allocator<std::pair<const unsigned int,BaseClass::A>>,
1>            _Moveit=std::_Tree<std::_Tmap_traits<unsigned int,BaseClass::A,std::less<unsigned int>,std::allocator<std::pair<const unsigned int,BaseClass::A>>,false>>::_Copy_tag
1>        ]
1>C:Program Files (x86)Microsoft Visual Studio 14.0VCincludextree(1104): note: see reference to function template instantiation 'void std::_Tree<std::_Tmap_traits<_Kty,_Ty,_Pr,_Alloc,false>>::_Copy<std::_Tree<std::_Tmap_traits<_Kty,_Ty,_Pr,_Alloc,false>>::_Copy_tag>(const std::_Tree<std::_Tmap_traits<_Kty,_Ty,_Pr,_Alloc,false>> &,_Moveit)' being compiled
1>        with
1>        [
1>            _Kty=unsigned int,
1>            _Ty=BaseClass::A,
1>            _Pr=std::less<unsigned int>,
1>            _Alloc=std::allocator<std::pair<const unsigned int,BaseClass::A>>,
1>            _Moveit=std::_Tree<std::_Tmap_traits<unsigned int,BaseClass::A,std::less<unsigned int>,std::allocator<std::pair<const unsigned int,BaseClass::A>>,false>>::_Copy_tag
1>        ]
1>C:Program Files (x86)Microsoft Visual Studio 14.0VCincludextree(1104): note: see reference to function template instantiation 'void std::_Tree<std::_Tmap_traits<_Kty,_Ty,_Pr,_Alloc,false>>::_Copy<std::_Tree<std::_Tmap_traits<_Kty,_Ty,_Pr,_Alloc,false>>::_Copy_tag>(const std::_Tree<std::_Tmap_traits<_Kty,_Ty,_Pr,_Alloc,false>> &,_Moveit)' being compiled
1>        with
1>        [
1>            _Kty=unsigned int,
1>            _Ty=BaseClass::A,
1>            _Pr=std::less<unsigned int>,
1>            _Alloc=std::allocator<std::pair<const unsigned int,BaseClass::A>>,
1>            _Moveit=std::_Tree<std::_Tmap_traits<unsigned int,BaseClass::A,std::less<unsigned int>,std::allocator<std::pair<const unsigned int,BaseClass::A>>,false>>::_Copy_tag
1>        ]
1>C:Program Files (x86)Microsoft Visual Studio 14.0VCincludextree(1095): note: while compiling class template member function 'std::_Tree<std::_Tmap_traits<_Kty,_Ty,_Pr,_Alloc,false>> &std::_Tree<std::_Tmap_traits<_Kty,_Ty,_Pr,_Alloc,f

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

std::thread objects are not copyable objects, the copy constructor is deleted:

thread( const thread& ) = delete; (since C++11)

This as a member of the class A, makes the class A objects also not copyable, deletes the copy constructor of A. Finally, it makes std::pair not copyable, deletes its copy constructor, as the error reports. Thus A can't be a value in a map. EXPORT_API does not matter.

You may use raw or shared pointers to std::thread in A.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...