文件 basic_socket.hpp 中 lib boost asio 1.47.0 错误

发布于 2024-12-14 13:36:42 字数 1091 浏览 1 评论 0原文

当我尝试编译以下代码时,我在以下代码中出现错误:

void        Server::accept(void)
{
    Network::ptr connection = Network::initialize(this->my_acceptor.get_io_service());
    this->my_acceptor.async_accept(connection->socket(), bind(&Server::endCmd, this, *connection, placeholders::error));
}

void        Server::endCmd(Network connection, const boost::system::error_code& error)
{
    if (!error)
        {
            std::cout << "success!" << std::endl;
            connection.start();
            this->accept();
        }
}

VC++ 2010 告诉我以下错误:

Error   1   error C2248: 'boost::asio::basic_io_object<IoObjectService>::basic_io_object' : cannot access private member declared in class 'boost::asio::basic_io_object<IoObjectService>'

我知道此错误出现在这一行,因为当我评论它时,错误消失了... 经过一番研究,当我调用 connection->getSocket() 时,可能与套接字的类有关,但此函数返回对套接字实例的引用:

tcp::socket& Network::socket(void)
{
    return (this->my_socket);
}

所以我在网上没有找到任何解决方案:(

有人有想法吗?

i have an error in the following code when i tried to compile this:

void        Server::accept(void)
{
    Network::ptr connection = Network::initialize(this->my_acceptor.get_io_service());
    this->my_acceptor.async_accept(connection->socket(), bind(&Server::endCmd, this, *connection, placeholders::error));
}

void        Server::endCmd(Network connection, const boost::system::error_code& error)
{
    if (!error)
        {
            std::cout << "success!" << std::endl;
            connection.start();
            this->accept();
        }
}

VC++ 2010 tell me the following error :

Error   1   error C2248: 'boost::asio::basic_io_object<IoObjectService>::basic_io_object' : cannot access private member declared in class 'boost::asio::basic_io_object<IoObjectService>'

i know this error come to this line because when i comment it, the error disapear...
After some research, it's probably with the socket's class when i call connection->getSocket() but this function returns a ref to an instance of socket :

tcp::socket& Network::socket(void)
{
    return (this->my_socket);
}

so i didn't find any solution on the web :(

Anyone have an idea plz ?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

绿萝 2024-12-21 13:36:42

async_accept是你自己写的吗?如果是这样,请确保它需要对套接字的引用,而不是按值传递。您收到的错误是说您正在尝试复制构造函数,并且复制构造函数被声明为私有(这是强制类不支持复制的 C++ 方法)。

Is async_accept something you wrote yourself? If so, make sure it takes a REFERENCE to socket, and not pass by value. The error you're getting is saying that you're trying to copy construct, and the copy constructor is declared private (this is a the C++ way of enforcing that the class doesn't support copying).

东走西顾 2024-12-21 13:36:42

我也遇到了这个问题,我花了几个小时看看发生了什么。我的案例是:

  • A 类,包含升压插座。 A 类它被用作 B 类的成员。B
  • 类是一个指针,并且它是不可复制的。成员 A 被声明为 B 类中的引用。

B 类中的原始代码是:

std::bind(&A::a_member, a_instance)

通过使用 a_instance 的地址解决了(当然)问题:

std::bind(&A::a_member, &a_instance).

我没有注意到这一点,我花了一段时间来解决这个问题。我希望它也能帮助其他人。

I had that problem too, and i spend a few hours to see what happened. My case was:

  • Class A which contains a boost socket. The class A it was used as a member in class B.
  • class B was a pointer and it was not copyable. Member A was declared as a reference in the B class.

Original code in the B class was:

std::bind(&A::a_member, a_instance)

The problem was fixed (and of course) by using the address of the a_instance:

std::bind(&A::a_member, &a_instance).

I did not notice that, and I took me a while to resolve this. I hope that it will be help others too.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文