我剛剛在我的新Linux環境中安裝了CLion,我決定嘗試製作一個簡單的套接字服務器。我最終想用C++創建一個套接字服務器(因爲我已經在C#,Java,Python,PHP,Node.js中做了大量的工作)。C++ Boost ASIO套接字錯誤
我得到了下面的代碼:
//
// 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
在我的主.cpp文件(程序執行時被調用):
#include "test.h"
int main() {
test t();
return 0;
}
最後,我的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_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 :: error)); –
看起來套接字是不可複製的,並且您通過值作爲「綁定」的第三個參數傳遞它。你真的需要學習如何閱讀這些錯誤:你粘貼的第一部分是複製構造函數的標準簽名。 – Useless