简体   繁体   English

错误:没有匹配函数可调用默认副本构造函数?

[英]error: no matching function for call to default copy constructor?

I have a std::map container variable in my class that is populated with objects of my nested-class : 我的类中有一个std::map容器变量,其中填充了我的嵌套类的对象:

class Logger {
private:
//...
    class Tick{
        ///stores start and end of profiling
        uint32_t start, lastTick,total;
        /// used for total time
        boost::mutex mutexTotalTime;
        ///is the profiling object started profiling?
        bool started;
    public:
        Tick(){
            begin();
        }
        /*
        Tick(const Tick &t){
            start = t.start;
            lastTick = t.lastTick;
            total = t.total;
            started = t.started;
        }
        */
        uint32_t begin();
        uint32_t end();
        uint32_t tick(bool addToTotalTime = false);
        uint32_t addUp(uint32_t value);
        uint32_t getAddUp();

    };
    std::map<const std::string, Tick> profilers_;
//...
public:
//...
Logger::Tick & Logger::getProfiler(const std::string id)
{
    std::map<const std::string, Tick>::iterator it(profilers_.find(id));
    if(it != profilers_.end())
    {
        return it->second;
    }
    else
    {
        profilers_.insert(std::pair<const std::string, Tick>(id, Tick()));
        it = profilers_.find(id);
    }
    return it->second;
}
//...
};

the above code will not compile if I dont provide a copy constructor while I thought the default copy constructor should be already in place?! 如果我不提供副本构造函数,而我认为默认副本构造函数应该已经就位,则上述代码将无法编译? Am I missing any concept? 我想念任何概念吗? thanks 谢谢

The copy constructor may be generated for you only if all members of your class are copyable. 仅当类的所有成员都是可复制的时,才可以为您生成复制构造函数。 In the case of Tick you have an object 如果是“ Tick您有一个物体

boost::mutex mutexTotalTime;

which is not copyable, so the compiler will not generate the copy constructor. 这是不可复制的,因此编译器将不会生成复制构造函数。 Observe that in your commented out copy constructor you do not copy the mutex - because you know you should not. 请注意,在注释掉的复制构造函数中,您不会复制互斥体-因为您知道不应该这样做。 The compiler does not know that. 编译器不知道这一点。

As a side note, there is no need to explicitly say const for map keys: 附带说明一下,不需要为映射键明确声明const

std::map<const std::string, Tick> profilers_;

Map keys are always const, and your declaration is completely equivalent to 映射键始终为const,并且您的声明完全等同于

std::map<std::string, Tick> profilers_;

boost::mutex is non-copyable. boost :: mutex是不可复制的。 Since Tick has one as a data member, this makes Tick also non-copyable. 由于Tick具有一个作为数据成员,因此这使得Tick也不可复制。 This in turn makes the map non-copyable. 反过来,这使得地图不可复制。

So to make Logger copyable, you have to provide your own copy constructor and implement an appropriate duplication of profilers_ in it. 因此,要使Logger复制,您必须提供自己的复制构造函数,并在其中构造profilers_的适当副本。 Or, perhaps even more appropriately (thanks to @LightnessRacesInOrbit for the suggestion), provide an appropriate copy constructor for Tick instead. 或者,也许更合适(由于使用@LightnessRacesInOrbit的建议),而是为Tick提供合适的副本构造函数。

The problem is that boost::mutex is not copyable. 问题是boost :: mutex是不可复制的。 So if you don't provide a copy constructor, the compiler attempts the generate a default one. 因此,如果您不提供复制构造函数,则编译器会尝试生成默认构造函数。 That default one needs to copy all of the members, but cannot copy boost::mutex, so it gives up. 这个默认值需要复制所有成员,但是不能复制boost :: mutex,所以它放弃了。 Your copy constructor does not copy the mutex. 您的复制构造函数不会复制互斥体。 Instead it default initializes the new one, so this works. 相反,它默认初始化新的,因此可以正常工作。

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

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