2

我对 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从函数中删除 s ,但是当将参数传递给make_shared.

  1. 有人可以向我解释这个错误是怎么回事吗?以及为什么 mingw 对这段代码没有问题?(我相信它在 VS10 和标准 gcc 上也能正常工作)。
  2. 没有很好的解释,我该如何解决这个问题并转移到下一个错误?

谢谢!

4

1 回答 1

2

好的,结果是阅读文档以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以将它们传递给非常量。

于 2013-06-03T20:36:58.437 回答