我对 C++ 还很陌生,所以我试图弄清楚这里到底发生了什么。我正在尝试编译(其他人的)代码。它使用 mingw 运行良好,但我也在嵌入式系统(TS-7800)上交叉编译并遇到编译器错误。在我看来,mingw 似乎以某种方式与 const 进行了一些自动匹配,而交叉编译器则没有,但我并不完全知道发生了什么。
代码如下:
typedef ::zmq::context_t ZeroMQContextType;
typedef boost::shared_ptr<ZeroMQContextType> ZeroMQContextHandleType;
typedef ::zmq::socket_t ZeroMQSocketType;
typedef boost::shared_ptr<ZeroMQSocketType> ZeroMQSocketHandleType;
SocketFactory::ZeroMQSocketHandleType SocketFactory::createZmqSocket(
ZeroMQContextHandleType const & contextHandle, int const & zmqSocketType) {
ZeroMQSocketHandleType socketHandle;
switch (zmqSocketType) {
case ZMQ_PUB:
socketHandle = boost::make_shared<ZeroMQSocketType>(*contextHandle, ZMQ_PUB);
// other stuff
// etc ...
}
return socketHandle;
}
我收到的错误:
/path/include/boost/smart_ptr/make_shared_object.hpp: In function `typename boost::detail::sp_if_not_array<T>::type boost::make_shared(const A1&, const A2&) [with T = zmq::socket_t, A1 = zmq::context_t,
SocketFactory.cpp:42: instantiated from here
/path/include/boost/smart_ptr/make_shared_object.hpp:743: error: no matching function for call to `zmq::socket_t::socket_t(const zmq::context_t&, const int&)'
/path/include/zmq.hpp:395: note: candidates are: zmq::socket_t::socket_t(const zmq::socket_t&)
/path/include/zmq.hpp:278: note: zmq::socket_t::socket_t(zmq::context_t&, int)
我尝试从函数中删除 const
,但看起来它们在将参数传递给 make_shared
时会立即添加回来。
- 有人可以向我解释一下这个错误是怎么回事吗?而且为什么mingw这段代码没有问题呢? (我相信它在 VS10 和标准 gcc 上也运行良好)。
- 如果没有很好的解释,我该如何解决此问题并继续处理下一个错误?
谢谢!
最佳答案
好吧,事实证明,阅读 make_shared
的文档就可以清除它。显然,如果检测到 C++0x 支持,make_shared
仅传递 const
引用:
http://www.boost.org/doc/libs/1_43_0/libs/smart_ptr/make_shared.html#functions
如前所述,可以将参数包装在 boost::ref
中以将它们传递为非常量。
关于c++ - 编译器特定错误 : can't match function with const arguments,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16905206/