C++ Boost ASIO: no matching member function for call to 'write_some'
Я хочу написать сокет, который будет устаналивать защищенное соединение. Написал такой код:
#pragma once
#include <boost/asio.hpp>
#include <cstdint>
#include <string>
using boost::asio::ip::tcp;
class SecureSocket
{
public:
SecureSocket(boost::asio::io_context ctx, int port, uint32_t encryptionKey = 123);
int send(const std::string& msg) const;
int send(const void* msg, std::size_t size);
int recv(std::string& msg, int length, double timeoutSecs = -1.0) const;
int recv(void* msg, int length, double timeoutSecs = -1.0) const;
private:
tcp::socket socket;
int port;
std::uint32_t encryptionKey;
std::string encrypt(const void* message, std::size_t size) const;
std::string decrypt(const void* message, std::size_t size) const;
};
И реализация
#include "secure_socket.h"
SecureSocket::SecureSocket(boost::asio::io_context ctx, int port, uint32_t encryptionKey)
: socket(ctx, tcp::v4()), port(port), encryptionKey(encryptionKey)
{
}
std::string SecureSocket::encrypt(const void *message, std::size_t size) const
{
std::string msg;
std::string encryptedMsg;
for (int i = 0; i < size; i++)
{
msg.push_back(static_cast<const char *>(message)[i]);
}
return msg;
}
std::string SecureSocket::decrypt(const void *message, std::size_t size) const
{
return SecureSocket::encrypt(message, size);
}
int SecureSocket::send(const std::string &msg) const
{
auto encryptedMsg = encrypt(msg.c_str(), msg.size());
boost::system::error_code ignoredError;
return boost::asio::write(socket, boost::asio::buffer(encryptedMsg));
}
Но этот код не компилируется, возникает ошибка
> /home/aoizora/QtCreator/Bot/socket/src/secure_socket.cpp:33: error: no
> matching member function for call to 'write_some' In file included
> from /home/aoizora/QtCreator/Bot/socket/src/secure_socket.cpp:1: In
> file included from
> /home/aoizora/QtCreator/Bot/socket/include/secure_socket.h:3: In file
> included from /usr/include/boost/asio.hpp:43: In file included from
> /usr/include/boost/asio/buffered_stream.hpp:22: In file included from
> /usr/include/boost/asio/buffered_write_stream.hpp:28: In file included
> from /usr/include/boost/asio/write.hpp:1246:
> /usr/include/boost/asio/impl/write.hpp:55:23: error: no matching
> member function for call to 'write_some'
> tmp.consume(s.write_some(tmp.prepare(max_size), ec));
> ~~^~~~~~~~~~ /usr/include/boost/asio/impl/write.hpp:71:18: note: in instantiation
> of function template specialization
> 'boost::asio::detail::write_buffer_sequence<const
> boost::asio::basic_stream_socket<boost::asio::ip::tcp>,
> boost::asio::mutable_buffers_1, const boost::asio::mutable_buffer *,
> boost::asio::detail::transfer_all_t>' requested here return
> detail::write_buffer_sequence(s, buffers,
> ^ /usr/include/boost/asio/impl/write.hpp:83:35: note: in instantiation of function template specialization
> 'boost::asio::write<const
> boost::asio::basic_stream_socket<boost::asio::ip::tcp>,
> boost::asio::mutable_buffers_1, boost::asio::detail::transfer_all_t>'
> requested here std::size_t bytes_transferred = write(s, buffers,
> transfer_all(), ec);
> ^ /home/aoizora/QtCreator/Bot/socket/src/secure_socket.cpp:33:25: note:
> in instantiation of function template specialization
> 'boost::asio::write<const
> boost::asio::basic_stream_socket<boost::asio::ip::tcp>,
> boost::asio::mutable_buffers_1>' requested here
> return boost::asio::write(socket, boost::asio::buffer(encryptedMsg));
> ^ /usr/include/boost/asio/basic_stream_socket.hpp:803:15: note:
> candidate function template not viable: 'this' argument has type
> 'const boost::asio::basic_stream_socket<boost::asio::ip::tcp>', but
> method is not marked const std::size_t write_some(const
> ConstBufferSequence& buffers,
> ^ /usr/include/boost/asio/basic_stream_socket.hpp:777:15: note:
> candidate function template not viable: requires single argument
> 'buffers', but 2 arguments were provided std::size_t
> write_some(const ConstBufferSequence& buffers)
> ^
Почему ошибка? Я вроде все делаю правильно. Как исправить код?
Источник: Stack Overflow на русском