错误 LNK2019:无法解析的外部符号“public: __thiscall Server::Server(class boost::asio::io_service &)
使用 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
这意味着您将
Server
的构造函数定义放入的任何源文件都不会被编译和/或链接。This means that whatever source file you put
Server
's constructor definition into isn't being compiled and/or linked.