簡體   English   中英

C++ 和 boost 1.71 - 錯誤:對“_1”的引用不明確

[英]C++ and boost 1.71 - error: reference to ‘_1’ is ambiguous

我正在使用庫 websocketcpp 和 boost 1.71。 代碼曾經與 boost 1.58 一起使用,但在升級兩個庫后,它將無法編譯。 C++編譯器為g++(Ubuntu 9.3.0-17ubuntu1~20.04)9.3.0,代碼如下:

using boost::property_tree::ptree;
using boost::property_tree::read_json;
using boost::property_tree::write_json;

using websocketpp::connection_hdl;
using websocketpp::lib::placeholders::_1;
using websocketpp::lib::placeholders::_2;
using websocketpp::lib::bind;
//.......
{
    srv.init_asio();
    srv.set_reuse_addr(true);
    srv.set_open_handler(bind(&WsServer::on_open, this, websocketpp::lib::placeholders::_1));
    srv.set_close_handler(bind(&WsServer::on_close, this, websocketpp::lib::placeholders::_1));
    srv.set_message_handler(bind(&WsServer::on_message, this, websocketpp::lib::placeholders::_1, websocketpp::lib::placeholders::_2));
}

我使用#define BOOST_BIND_NO_PLACEHOLDERS進行了#define BOOST_BIND_NO_PLACEHOLDERS 但是,這將給出以下錯誤:

/usr/include/boost/bind/bind.hpp:319:35: error: no match for call to ‘(boost::_mfi::mf1<void, boost::property_tree::json_parser::detail::standard_callbacks<boost::property_tree::basic_ptree<std::__cxx11::basic_string<char>, std::__cxx11::basic_string<char> > >, char>) (
boost::property_tree::json_parser::detail::standard_callbacks<boost::property_tree::basic_ptree<std::__cxx11::basic_string<char>, std::__cxx11::basic_string<char> > >&, std::_Placeholder<1>&)’
  319 |         unwrapper<F>::unwrap(f, 0)(a[base_type::a1_], a[base_type::a2_]);
      |         ~~~~~~~~~~~~~~~~~~~~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
//...
/usr/include/boost/bind/mem_fn_template.hpp:176:25: note:   template argument deduction/substitution failed:

usr/include/boost/bind/mem_fn_template.hpp:184:7: note: candidate: ‘R boost::_mfi::mf1<R, T, A1>::operator()(T&, A1) const [with R = void; T = boost::property_tree::json_parser::detail::standard_callbacks<boost::property_tree::basic_ptree<std::__cxx11::basic_string<char>, std::__cxx11::basic_string<char> > >; A1 = char]’
  184 |     R operator()(T & t, A1 a1) const
      |       ^~~~~~~~
/usr/include/boost/bind/mem_fn_template.hpp:184:28: note:   no known conversion for argument 2 from ‘std::_Placeholder<1>’ to ‘char’
  184 |     R operator()(T & t, A1 a1) const

就像大家說的,不要using namespace

此外,使用最近的 boost 你應該避免BOOST_BIND_GLOBAL_PLACEHOLDERS ,而不是<boost/bind.hpp>包括<boost/bind/bind.hpp> (默認情況下不再這樣做。

這是您看到的沖突的根源,因為property_tree使用 boost 綁定(如錯誤消息所示)。

完全不相關:不要 (ab) 使用 PropertyTree,就好像它是一個 JSON 庫一樣。 而是使用 Boost JSON 或其他一些 JSON 庫。


此外,無關,無論如何您都不需要所有using 您已經可以使用 ADL 來減少輸入:

 #include <boost/property_tree/json_parser.hpp> #include <iostream> int main() { boost::property_tree::ptree pt; std::istringstream str(R"({"hello":"world"})"); read_json(str, pt); write_json(std::cout, pt); }

觀看Coliru 直播

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM