Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/sockets/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ C++;Boost ASIO套接字错误_C++_Sockets_C++11_Boost_Cmake - Fatal编程技术网

C++ C++;Boost ASIO套接字错误

C++ C++;Boost ASIO套接字错误,c++,sockets,c++11,boost,cmake,C++,Sockets,C++11,Boost,Cmake,我今天刚刚在我的新Linux环境中安装了CLion,我决定尝试制作一个简单的套接字服务器。我最终想在C++中创建一个套接字服务器(因为我已经在C语言,java,python,php,No.js……中做了很多)。 我得到了以下代码: // // Created by josh on 10-10-16. // #ifndef RANDOMPROGRAM_TEST_H #define RANDOMPROGRAM_TEST_H #include <iostream> #include &

我今天刚刚在我的新Linux环境中安装了CLion,我决定尝试制作一个简单的套接字服务器。我最终想在C++中创建一个套接字服务器(因为我已经在C语言,java,python,php,No.js……中做了很多)。 我得到了以下代码:

//
// 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
最后,我的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})
现在,当我尝试执行该程序时,它会出现以下错误,可能还有大量错误:

No matching function for call to ‘boost::asio::basic_socket_acceptor<boost::asio::ip::tcp>::basic_socket_acceptor()’
调用“boost::asio::basic_socket_acceptor::basic_socket_acceptor()”时没有匹配的函数 日志:

当我试图执行

你是说编译,对吗?这是一个编译错误,不是运行时错误

调用'boost::asio::basic_socket_acceptor::basic_socket_acceptor()'时没有匹配的函数。

此处记录了的构造函数。没有默认构造函数,这是编译器告诉您的

您正在调用(或尝试调用)此处的默认构造函数:

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));
}
其中没有初始值设定项列表。
test
的任何数据成员必须在构造函数主体之前构造

您的构造函数应该如下所示:

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));
}

它修复了错误,但我仍然有一些错误。我看到的一个是:错误:使用删除的函数'boost::asio::basic_stream_socket::basic_stream_socket(const boost::asio::basic_stream_socket&)'acceptor.async_accept(this->socket,boost::bind(&test::handle_accept,this,this->->socket,boost::asio::placeholders::placeholder::error));
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));
}