错误 LNK2019:无法解析的外部符号“public: __thiscall Server::Server(class boost::asio::io_service &)

发布于 2024-11-08 08:41:01 字数 1959 浏览 0 评论 0原文

使用 boost::asio 的以下代码将无法编译:

    #ifndef _SERVER_H_
    #define _SERVER_H_

    #include "Connection.h"

    class Server
    {
    public:
      Server(boost::asio::io_service& io_service);

    private:
      void start_accept();

      void handle_accept(Connection::pointer new_connection,const boost::system::error_code& error);


      boost::asio::ip::tcp::acceptor acceptor_;
    };

    #endif
    -------------------------------------------------------------------------------------------------
    #include "Server.h"

    Server::Server(boost::asio::io_service& io_service)
        : acceptor_(io_service, boost::asio::ip::tcp::endpoint(boost::asio::ip::tcp::v4(), 9985)){
            start_accept();
    }

    void Server::start_accept(){

        Connection::pointer new_connection =
          Connection::create(acceptor_.io_service());

        acceptor_.async_accept(new_connection->socket(),
            boost::bind(&Server::handle_accept, this, new_connection,
              boost::asio::placeholders::error));
    }


    void Server::handle_accept(Connection::pointer new_connection,const boost::system::error_code& error){
        if (!error)
        {
          new_connection->start();
          start_accept();
        }
      }
--------------------------------------------------------------------------------------------------
#include <Server.h>
#include <iostream>


int main()
{
  try
  {
    boost::asio::io_service io_service;
    Server server1(io_service);
    io_service.run();
  }
  catch (std::exception& e)
  {
    std::cerr << e.what() << std::endl;
  }

  return 0;
}

这是它在 Visual C++ 中产生的错误:

error LNK2019: unresolved external symbol "public: __thiscall Server::Server(class boost::asio::io_service &)" (??0Server@@QAE@AAVio_service@asio@boost@@@Z) referenced in function _main

这个错误是什么意思?

The following code using boost::asio will not compile:

    #ifndef _SERVER_H_
    #define _SERVER_H_

    #include "Connection.h"

    class Server
    {
    public:
      Server(boost::asio::io_service& io_service);

    private:
      void start_accept();

      void handle_accept(Connection::pointer new_connection,const boost::system::error_code& error);


      boost::asio::ip::tcp::acceptor acceptor_;
    };

    #endif
    -------------------------------------------------------------------------------------------------
    #include "Server.h"

    Server::Server(boost::asio::io_service& io_service)
        : acceptor_(io_service, boost::asio::ip::tcp::endpoint(boost::asio::ip::tcp::v4(), 9985)){
            start_accept();
    }

    void Server::start_accept(){

        Connection::pointer new_connection =
          Connection::create(acceptor_.io_service());

        acceptor_.async_accept(new_connection->socket(),
            boost::bind(&Server::handle_accept, this, new_connection,
              boost::asio::placeholders::error));
    }


    void Server::handle_accept(Connection::pointer new_connection,const boost::system::error_code& error){
        if (!error)
        {
          new_connection->start();
          start_accept();
        }
      }
--------------------------------------------------------------------------------------------------
#include <Server.h>
#include <iostream>


int main()
{
  try
  {
    boost::asio::io_service io_service;
    Server server1(io_service);
    io_service.run();
  }
  catch (std::exception& e)
  {
    std::cerr << e.what() << std::endl;
  }

  return 0;
}

This is the error it produces in Visual C++:

error LNK2019: unresolved external symbol "public: __thiscall Server::Server(class boost::asio::io_service &)" (??0Server@@QAE@AAVio_service@asio@boost@@@Z) referenced in function _main

What does this error mean?

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

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

发布评论

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

评论(1

冷清清 2024-11-15 08:45:26

这意味着您将 Server 的构造函数定义放入的任何源文件都不会被编译和/或链接。

This means that whatever source file you put Server's constructor definition into isn't being compiled and/or linked.

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