【问题标题】:C++ Boost: initializing endpoint after contructorC++ Boost:在构造函数之后初始化端点
【发布时间】:2018-03-13 02:34:42
【问题描述】:

我尝试创建一个使用 UDP 的类。我想在构造函数之后初始化端点,所以我修改如下:

class UdpSender
{
private:
    boost::asio::ip::udp::endpoint endpoint;
    boost::asio::ip::udp::socket socket;

    string multicast_address;
    unsigned short multicast_port;
    boost::thread_group threads;    // thread group
    boost::thread* thread_main;     // main thread
    boost::thread* thread_listen;   // listen thread
    boost::thread* thread_getsend;  // get/send thread
    boost::mutex stopMutex;
    bool initialize = false;
    bool stop, showBroadcast;
    int i_getsend, i_listen, i_main, i_message, interval;
    string message;
public:
    // constructor
    UdpSender(boost::asio::io_service& io_service, std::string multicast_address, unsigned short multicast_port, int interval, bool show = false)
        : 
        //endpoint(boost::asio::ip::address::from_string(multicast_address), multicast_port),
        //socket(io_service, endpoint.protocol()),
        multicast_address(multicast_address),
        multicast_port(multicast_port),
        interval(interval),
        showBroadcast(show)
    {
        initialize = true;
        Initialize(io_service);
    }

    UdpSender(boost::asio::io_service& io_service)
    {
        initialize = true;
        Initialize();
    }
    // destructor
    ~UdpSender()
    {
        // show exit message
        cout << "Exiting UDP Communicator." << endl;
    }

    // initialize
    void Initialize(boost::asio::io_service& io_service)
    {
        if (initialize == false)
        {
            GetInfo();
        }

        boost::asio::ip::udp::endpoint endpoint(boost::asio::ip::make_address(multicast_address), multicast_port);
        boost::asio::ip::udp::socket socket(io_service, endpoint.protocol());
        socket.set_option(boost::asio::ip::udp::socket::reuse_address(true));   // no need

        thread_main = new boost::thread(boost::ref(*this));
        //thread_listen = new boost::thread(&UdpSender::Callable_Listen, this, interval, boost::ref(i_listen));
        //threads.add_thread(thread_listen);    // listen thread
        thread_getsend = new boost::thread(&UdpSender::Callable_GetSend, this, interval, boost::ref(i_listen), boost::ref(message));
        threads.add_thread(thread_getsend); // get/send thread
        stop = false;
        i_getsend = 0;
        i_listen = 0;
        i_main = 0;
        i_message = 0;
        message.clear();

        initialize = true;
    }
    // ============ other things removed to shorten code ============
};    

显然,它不喜欢从原始构造函数中注释掉的这两行:

//endpoint(boost::asio::ip::address::from_string(multicast_address), multicast_port),
//socket(io_service, endpoint.protocol()),

也不喜欢新的构造函数:

UdpSender(boost::asio::io_service& io_service)

我能做些什么来让这一切成为可能?我真的很感谢你的帮助。谢谢。

【问题讨论】:

    标签: c++ boost udp boost-asio endpoint


    【解决方案1】:

    没有。 According to the Asio reference,端点对象应通过其constructors进行配置。

    但是,您可以通过 [smart-] 指针持有 endpoint,并在 UdpSender 构造之后创建它。

    // defining:
    private:
        boost::shared_ptr<boost::asio::ip::udp::endpoint> endpoint;
    
    // creating in Initialize()
    endpoint = boost::make_shared<boost::asio::ip::udp::endpoint>(boost::asio::ip::address::from_string(multicast_address), multicast_port);
    

    【讨论】:

    • 我试过那行,抛出这个错误:没有运算符“=”匹配这些操作数。
    • @CaTx 确保您的endpoint 定义正确。发布确切的编译错误。
    • 1.没有运算符“=”匹配这些操作数
    • 2.二进制“=”:未找到采用“boost::shared_ptr”类型的右侧操作数的运算符(或没有可接受的转换)
    • @CaTx 所以你没有正确定义endpoint。应该是boost::shared_ptr&lt;boost::asio::ip::udp::endpoint&gt; endpoint
    猜你喜欢
    • 1970-01-01
    • 2011-03-28
    • 1970-01-01
    • 2018-08-02
    • 2018-08-21
    • 1970-01-01
    • 2018-06-30
    • 2021-06-30
    • 1970-01-01
    相关资源
    最近更新 更多