在头文件(我没有编写)中,已经定义了一个结构体,如下所示
struct MemoryMessage : public boost::counted_base { /*, public FastAlloc*/
explicit MemoryMessage(MemoryMessageType aType)
: theType(aType) {}
explicit MemoryMessage(MemoryMessageType aType, MemoryAddress anAddress)
: theType(aType) {}
explicit MemoryMessage(MemoryMessageType aType, MemoryAddress anAddress, int anIdentifier)
: theType(aType) {}
explicit MemoryMessage(MemoryMessageType aType, MemoryAddress anAddress, VirtualMemoryAddress aPC)
: theType(aType) {}
explicit MemoryMessage(MemoryMessageType aType, MemoryAddress anAddress, VirtualMemoryAddress aPC, DataWord aData)
: theType(aType) {}
explicit MemoryMessage(MemoryMessage & aMsg)
: theType(aMsg.theType) {}
}
后来在我的代码中,我写了
MemoryMessage testMsg;
class foo() {
foo()
: testMsg(MemoryMessage::test)
{}
std::vector< MemoryMessage > candidates;
void bar() {
candidates.push_back(testMsg);
}
}
但我收到这个错误
error: no matching function for call to 'MemoryMessage::MemoryMessage(const MemoryMessage&)’
note: candidates are:MemoryMessage::MemoryMessage(MemoryMessage&)
这有什么问题吗?我创建了一个非常小的片段。如果我的解释中遗漏了某些内容,请告诉我。
幕后的这一行调用了一个复制构造函数:
candidates.push_back(testMsg);
The testMsg
that push_back
经过const
通过调用内部的复制构造函数将引用放入向量中std::vector
的代码。但是,您的构造函数已声明explicit
, so std::vector
的代码无法访问它。
去除explicit
复制构造函数中的指示符将解决此问题。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)