如何实例化 boost 类`boost::posix_time::seconds` C++ 类?

Posted

技术标签:

【中文标题】如何实例化 boost 类`boost::posix_time::seconds` C++ 类?【英文标题】:How to instantiate the boost class `boost::posix_time::seconds` C++ class? 【发布时间】:2021-11-06 19:20:03 【问题描述】:

我在安装在/usr/include/boost 的boost 库1.71.0 中找到了以下类定义。

class BOOST_SYMBOL_VISIBLE seconds : public time_duration
        
            public:
                template <typename T>
                    explicit seconds(T const& s,
                            typename boost::enable_if<boost::is_integral<T>, void>::type* = BOOST_DATE_TIME_NULLPTR) :
                        time_duration(0,0, numeric_cast<sec_type>(s))
            
        ;

上面的类定义可以在/usr/include/boost/date_time/posix_time/posix_time_duration.hpp找到

我正在使用以下代码-sn-p,其中类 boost::posix_time::seconds 被实例化:

#include <boost/asio.hpp>
#include <boost/array.hpp>
#include <boost/format.hpp>
#include <boost/bind.hpp>
#include <boost/serialization/access.hpp>
#include <boost/archive/binary_iarchive.hpp>
#include <boost/shared_array.hpp>
#include <boost/algorithm/string.hpp>
#include <chrono>
#include <vector>
#include <boost/date_time/posix_time/posix_time_duration.hpp>

int main()

    boost::asio::io_service io_service;
    boost::asio::ip::tcp::socket socket(io_service);
    boost::asio::deadline_timer timer(io_service);

    timer.expires_from_now(boost::posix_time::seconds(60.0))

    return 0;


假设我用文件名boost.cpp 保存了上面的代码-sn-p。然后我使用命令编译boost.cpp

g++ -I /usr/include/boost  -pthread boost.cpp

但是,我收到以下错误:

boost.cpp: In function ‘int main()’:
boost.cpp:19:59: error: no matching function for call to ‘boost::posix_time::seconds::seconds(double)’
   19 |     timer.expires_from_now(boost::posix_time::seconds(60.0))
      |                                                           ^
In file included from /usr/include/boost/date_time/posix_time/posix_time_types.hpp:16,
                 from /usr/include/boost/asio/time_traits.hpp:23,
                 from /usr/include/boost/asio/detail/timer_queue_ptime.hpp:22,
                 from /usr/include/boost/asio/detail/deadline_timer_service.hpp:29,
                 from /usr/include/boost/asio/basic_deadline_timer.hpp:24,
                 from /usr/include/boost/asio.hpp:25,
                 from boost.cpp:1:
/usr/include/boost/date_time/posix_time/posix_time_duration.hpp:57:16: note: candidate: ‘template<class T> boost::posix_time::seconds::seconds(const T&, typename boost::enable_if<boost::is_integral<T>, void>::type*)’
   57 |       explicit seconds(T const& s,
      |                ^~~~~~~
/usr/include/boost/date_time/posix_time/posix_time_duration.hpp:57:16: note:   template argument deduction/substitution failed:
/usr/include/boost/date_time/posix_time/posix_time_duration.hpp: In substitution of ‘template<class T> boost::posix_time::seconds::seconds(const T&, typename boost::enable_if<boost::is_integral<T>, void>::type*) [with T = double]’:
boost.cpp:19:59:   required from here
/usr/include/boost/date_time/posix_time/posix_time_duration.hpp:57:16: error: no type named ‘type’ in ‘struct boost::enable_if<boost::is_integral<double>, void>’
/usr/include/boost/date_time/posix_time/posix_time_duration.hpp:53:30: note: candidate: ‘boost::posix_time::seconds::seconds(const boost::posix_time::seconds&)’
   53 |   class BOOST_SYMBOL_VISIBLE seconds : public time_duration
      |                              ^~~~~~~
/usr/include/boost/date_time/posix_time/posix_time_duration.hpp:53:30: note:   no known conversion for argument 1 from ‘double’ to ‘const boost::posix_time::seconds&’
/usr/include/boost/date_time/posix_time/posix_time_duration.hpp:53:30: note: candidate: ‘boost::posix_time::seconds::seconds(boost::posix_time::seconds&&)’
/usr/include/boost/date_time/posix_time/posix_time_duration.hpp:53:30: note:   no known conversion for argument 1 from ‘double’ to ‘boost::posix_time::seconds&&’

我能想到的是boost::posix_time::seconds(60.0) 行的使用不正确,但我无法根据上述定义找出实例化posix_time::seconds 类的正确方法。你们有谁知道吗?

【问题讨论】:

【参考方案1】:

该构造函数采用整数(整数)数。 boost::posix_time::seconds(60) 应该可以工作。

Reference

【讨论】:

以上是关于如何实例化 boost 类`boost::posix_time::seconds` C++ 类?的主要内容,如果未能解决你的问题,请参考以下文章

使用Armadillo和boost :: numeric :: odeint进行模板实例化

C++ 从类的构造函数运行 boost 线程

带有 boost 的模板实例化:传递额外的参数

C++ boost线程在实例化两次时导致分段错误

在类对象段错误中使用 boost::interprocess,为啥?

如何使用 PyObjects 声明 Boost.Python C++ 类