简体   繁体   English

C ++ Boost ASIO套接字错误

[英]C++ Boost ASIO socket errors

I just installed CLion today on my new Linux environment and I decided to try to make a simple socket server. 我今天刚刚在我的新Linux环境中安装了CLion,我决定尝试创建一个简单的套接字服务器。 I eventually want to create a socket server in C++ (because I already made tons in C#, Java, Python, PHP, Node.js...). 我最终想用C ++创建一个套接字服务器(因为我已经在C#,Java,Python,PHP,Node.js ......中创建了很多)。

I got the following code: 我得到以下代码:

//
// Created by josh on 10-10-16.
//

#ifndef RANDOMPROGRAM_TEST_H
#define RANDOMPROGRAM_TEST_H

#include <iostream>
#include <boost/bind.hpp>
#include <boost/asio.hpp>

using namespace boost::asio::ip;

class test {

private:
    boost::asio::io_service io_service;
    tcp::acceptor acceptor;
    tcp::socket socket;

    test() {
        this->acceptor = tcp::acceptor(this->io_service, tcp::endpoint(tcp::v4(), 30000));

        this->socket = tcp::socket(io_service);

        acceptor.async_accept(this->socket, boost::bind(&this->handle_accept, this, this->socket, NULL));
    }

    void handle_accept(tcp::socket* client, const boost::system::error_code& error) {

    }
};
#endif //RANDOMPROGRAM_TEST_H

In my main .cpp file (which gets called when the program is executed): 在我的主.cpp文件中(在程序执行时调用):

#include "test.h"

int main() {
    test t();
    return 0;
}

Finally, my CMakeLists.txt 最后,我的CMakeLists.txt

cmake_minimum_required(VERSION 3.6)
project(Randomshitprogram)

set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11")

find_package(Boost 1.62.0 COMPONENTS system filesystem REQUIRED)

if(Boost_FOUND)

    message(STATUS "Boost_INCLUDE_DIRS: ${Boost_INCLUDE_DIRS}")
    message(STATUS "Boost_LIBRARIES: ${Boost_LIBRARIES}")
    message(STATUS "Boost_VERSION: ${Boost_VERSION}")

    include_directories(${Boost_INCLUDE_DIRS})

endif()

set(SOURCE_FILES main.cpp test.h)
add_executable(Randomshitprogram ${SOURCE_FILES})

Now, when I try to execute the program, it gives the following error, together with possibly a ton of errors: 现在,当我尝试执行程序时,它会出现以下错误,并可能出现大量错误:

No matching function for call to ‘boost::asio::basic_socket_acceptor<boost::asio::ip::tcp>::basic_socket_acceptor()’

The log: 日志:

http://pastebin.com/a09FvuTk http://pastebin.com/a09FvuTk

when I try to execute 当我尝试执行时

you mean compile, right? 你的意思是编译,对吗? That's a compile error, not a runtime one. 这是编译错误,而不是运行时错误。

No matching function for call to 'boost::asio::basic_socket_acceptor<boost::asio::ip::tcp>::basic_socket_acceptor()'

The constructor for boost::asio::basic_socket_acceptor is documented here. boost :: asio :: basic_socket_acceptor的构造函数在此处记录。 There is no default constructor, which is what your compiler is telling you. 没有默认构造函数,这是编译器告诉你的。

You're invoking (or trying to) the default constructor here: 您在这里调用(或尝试)默认构造函数:

test() /* here */ {
    this->acceptor = tcp::acceptor(this->io_service, tcp::endpoint(tcp::v4(), 30000));

    this->socket = tcp::socket(io_service);

    acceptor.async_accept(this->socket, boost::bind(&this->handle_accept, this, this->socket, NULL));
}

where there is no initializer list. 没有初始化列表的地方。 Any data members of test must be constructed before the body of your constructor. 必须在构造函数的主体之前构造test任何数据成员。

Your constructor should look something like this: 你的构造函数应该是这样的:

test()
: acceptor(io_service, tcp::endpoint(tcp::v4(), 30000))
, socket(io_service)
{
    acceptor.async_accept(socket, boost::bind(&this->handle_accept, this, this->socket, NULL));
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM