使用异步函数 boost asio 编译错误
我想创建一个异步。服务器。 我成功做到了这一点,但现在我想将 async_read/asyn_write 函数绑定到调用者对象函数。所以我尝试用 boost::function 来做到这一点 这里有我的代码:
Server.cpp
#include "Server.h"
#include "Client.h"
#include "Network.h"
void Server::accept(void)
{
Network::ptr connection = Network::create(this->my_acceptor.get_io_service());
this->my_acceptor.async_accept(connection->getSocket(), bind(&Server::endCmd, this, connection, placeholders::error));
}
void Server::endCmd(Network::ptr connection, const boost::system::error_code& error)
{
if (!error)
{
std::cout << "Recu un client!" << std::endl;
this->client.push_back(new Client(connection));
this->client[client.size() - 1]->checkAuthentification();
std::cout << "Envoie du message de securisation" << std::endl;
std::cout << "Nouvelle ecoute" << std::endl;
this->accept();
}
}
Client.h 和 .cpp
#include "Network.h"
class Client
{
private:
Network::ptr connection;
public:
Client(Network::ptr);
~Client();
void checkAuthentification(void);
void endRead(const error_code& error, size_t nbytes);
void endWrite(const error_code &error);
};
#include "Client.h"
Client::Client(Network::ptr connect)
{
this->connection = connect;
this->connection->assignFunction(this);
}
Client::~Client()
{
this->connection->close();
}
void Client::checkAuthentification(void)
{
this->connection->Swrite("BIENVENUE", this);
}
void Client::endRead(const error_code& error, size_t nbytes)
{
if (!error && nbytes != 0)
{
std::cout << this->connection->getRcvMsg() << std::endl;
this->connection->Sread(this);
}
else
this->connection->close();
}
void Client::endWrite(const error_code &error)
{
if (!error)
this->connection->Sread(this);
else
this->connection->close();
}
以及 Network.cpp 和 .h
#include "Client.h"
class Network : public boost::enable_shared_from_this<Network>
{
private:
tcp::socket socket;
std::string rcv_msg;
std::string msg;
boost::array<char, 6> rbuffer;
boost::function<void (Client *, const error_code &, size_t)> fread;
boost::function<void (Client *, const error_code &)> fwrite;
public:
typedef boost::shared_ptr<Network> ptr;
Network(io_service &);
~Network();
void assignFunction(void);
void close(void);
void Sread(Client *cli);
void Swrite(std::string msg, Client *cli);
tcp::socket& getSocket(void);
std::string getRcvMsg(void);
static ptr create(io_service &);
};
#include "Client.h"
#include "Network.h"
Network::Network(io_service &ios) : socket(ios)
{
}
Network::~Network()
{
this->close();
}
void Network::assignFunction(void)
{
this->fread = &Client::endRead;
this->fwrite = &Client::endWrite;
}
void Network::close(void)
{
if (this->socket.is_open())
{
std::cout << "Connection closed" << std::endl;
this->socket.close();
}
}
void Network::Sread(Client *cli)
{
async_read(this->socket, buffer(this->rbuffer), bind(&Network::fread, cli, placeholders::error, placeholders::bytes_transferred));
}
void Network::Swrite(std::string msg, Client *cli)
{
this->msg = msg;
async_write(this->socket, buffer(this->msg, (int)this->msg.size()), bind(&Network::fwrite, cli, placeholders::error));
}
std::string Network::getRcvMsg(void)
{
return (std::string(this->rbuffer.c_array(), this->rbuffer.size()));
}
tcp::socket& Network::getSocket(void)
{
return (this->socket);
}
Network::ptr Network::create(io_service &ios)
{
return (ptr(new Network(ios)));
}
当我想编译它时,出现以下错误:
Client.h(10): error C2653: 'Network' : is not a class or namespace name
Client.h(10): error C2146: syntax error : missing ';' before identifier 'connection'
Client.h(10): error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
Client.h(10): error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
Client.h(13): error C2653: 'Network' : is not a class or namespace name
Client.h(13): error C2653: 'Network' : is not a class or namespace name
Client.h(13): error C2460: 'Client::ptr' : uses 'Client', which is being defined
Client.h(8) : see declaration of 'Client'
1>Client.cpp(6): error C2511: 'Client::Client(Network::ptr)' : overloaded member function not found in 'Client'
Client.h(8) : see declaration of 'Client'
1>Client.cpp(13): error C2039: 'connection' : is not a member of 'Client'
Client.h(8) : see declaration of 'Client'
1>Client.cpp(13): error C2227: left of '->close' must point to class/struct/union/generic type
1>Client.cpp(18): error C2039: 'connection' : is not a member of 'Client'
Client.h(8) : see declaration of 'Client'
1>Client.cpp(18): error C2227: left of '->Swrite' must point to class/struct/union/generic type
1>Client.cpp(25): error C2039: 'connection' : is not a member of 'Client'
Client.h(8) : see declaration of 'Client'
1>Client.cpp(25): error C2227: left of '->getRcvMsg' must point to class/struct/union/generic type
1>Client.cpp(26): error C2039: 'connection' : is not a member of 'Client'
Client.h(8) : see declaration of 'Client'
1>Client.cpp(26): error C2227: left of '->Sread' must point to class/struct/union/generic type
1>Client.cpp(29): error C2039: 'connection' : is not a member of 'Client'
Client.h(8) : see declaration of 'Client'
1>Client.cpp(29): error C2227: left of '->close' must point to class/struct/union/generic type
1>Client.cpp(35): error C2039: 'connection' : is not a member of 'Client'
Client.h(8) : see declaration of 'Client'
1>Client.cpp(35): error C2227: left of '->Sread' must point to class/struct/union/generic type
1>Client.cpp(37): error C2039: 'connection' : is not a member of 'Client'
Client.h(8) : see declaration of 'Client'
1>Client.cpp(37): error C2227: left of '->close' must point to class/struct/union/generic type
Commande.cpp
main.cpp
Client.h(10): error C2653: 'Network' : is not a class or namespace name
Client.h(10): error C2146: syntax error : missing ';' before identifier 'connection'
Client.h(10): error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
Client.h(10): error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
Client.h(13): error C2653: 'Network' : is not a class or namespace name
Client.h(13): error C2653: 'Network' : is not a class or namespace name
Client.h(13): error C2460: 'Client::ptr' : uses 'Client', which is being defined
Client.h(8) : see declaration of 'Client'
My_exception.cpp
Network.cpp
Network.h(14): error C2065: 'Client' : undeclared identifier
Network.h(14): error C2059: syntax error : ','
Network.h(15): error C2065: 'Client' : undeclared identifier
Network.h(15): error C2059: syntax error : ','
Network.h(25): error C2065: 'Client' : undeclared identifier
Network.h(25): error C2065: 'cli' : undeclared identifier
Network.h(25): error C2143: syntax error : missing ',' before ')'
Network.h(29): error C2143: syntax error : missing ';' before '}'
Client.h(8): error C2143: syntax error : missing ';' before '{'
Client.h(18): error C2143: syntax error : missing ';' before '}'
1>Network.cpp(6): error C2143: syntax error : missing ';' before '{'
1>Network.cpp(7): error C2143: syntax error : missing ';' before '}'
1>Network.cpp(10): error C2143: syntax error : missing ';' before '{'
1>Network.cpp(12): error C2143: syntax error : missing ';' before '}'
1>Network.cpp(15): error C2143: syntax error : missing ';' before '{'
1>Network.cpp(16): error C2653: 'Client' : is not a class or namespace name
1>Network.cpp(17): error C2653: 'Client' : is not a class or namespace name
1>Network.cpp(18): error C2143: syntax error : missing ';' before '}'
1>Network.cpp(21): error C2143: syntax error : missing ';' before '{'
1>Network.cpp(23): error C2143: syntax error : missing ';' before '{'
1>Network.cpp(26): error C2143: syntax error : missing ';' before '}'
1>Network.cpp(27): error C2143: syntax error : missing ';' before '}'
1>Network.cpp(30): error C2143: syntax error : missing ';' before '{'
1>Network.cpp(31): error C2355: 'this' : can only be referenced inside non-static member functions
1>Network.cpp(31): error C2227: left of '->rbuffer' must point to class/struct/union/generic type
1>Network.cpp(31): error C2065: 'cli' : undeclared identifier
1>Network.cpp(31): error C2143: syntax error : missing ',' before ')'
1>Network.cpp(32): error C2143: syntax error : missing ';' before '}'
1>Network.cpp(35): error C2065: 'Client' : undeclared identifier
1>Network.cpp(35): error C2065: 'cli' : undeclared identifier
1>Network.cpp(35): error C2143: syntax error : missing ',' before ')'
1>Network.cpp(36): error C2143: syntax error : missing ';' before '{'
1>Network.cpp(38): error C2355: 'this' : can only be referenced inside non-static member functions
1>Network.cpp(38): error C2227: left of '->msg' must point to class/struct/union/generic type
1>Network.cpp(38): error C2355: 'this' : can only be referenced inside non-static member functions
1>Network.cpp(38): error C2227: left of '->msg' must point to class/struct/union/generic type
1>Network.cpp(38): error C2228: left of '.size' must have class/struct/union
1>Network.cpp(38): error C2065: 'cli' : undeclared identifier
1>Network.cpp(38): error C2143: syntax error : missing ',' before ')'
1>Network.cpp(39): error C2143: syntax error : missing ';' before '}'
1>Network.cpp(42): error C2143: syntax error : missing ';' before '{'
1>Network.cpp(43): error C2355: 'this' : can only be referenced inside non-static member functions
1>Network.cpp(43): error C2227: left of '->rbuffer' must point to class/struct/union/generic type
1>Network.cpp(43): error C2228: left of '.size' must have class/struct/union
1>Network.cpp(43): error C2143: syntax error : missing ',' before ')'
1>Network.cpp(44): error C2143: syntax error : missing ';' before '}'
1>Network.cpp(47): error C2143: syntax error : missing ';' before '{'
1>Network.cpp(49): error C2143: syntax error : missing ';' before '}'
1>Network.cpp(52): error C2143: syntax error : missing ';' before '{'
1>Network.cpp(54): error C2143: syntax error : missing ';' before '}'
1>Network.cpp(55): error C2143: syntax error : missing ';' before '}'
1>Network.cpp(55): fatal error C1004: unexpected end-of-file found
Server.cpp
Client.h(10): error C2653: 'Network' : is not a class or namespace name
Client.h(10): error C2146: syntax error : missing ';' before identifier 'connection'
Client.h(10): error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
Client.h(10): error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
Client.h(13): error C2653: 'Network' : is not a class or namespace name
Client.h(13): error C2653: 'Network' : is not a class or namespace name
Client.h(13): error C2460: 'Client::ptr' : uses 'Client', which is being defined
Client.h(8) : see declaration of 'Client'
1>Server.cpp(22): error C2664: 'Client::Client(const Client &)' : cannot convert parameter 1 from 'Network::ptr' to 'const Client &'
Reason: cannot convert from 'Network::ptr' to 'const Client'
No user-defined-conversion operator available that can perform this conversion, or the operator cannot be called
经过这么多小时的“谷歌搜索”和反思,我没有找到了为什么我有这些错误。 任何人都可以帮助我吗?
I want to create an async. server.
i succeed to do that, but now i want to bind async_read/asyn_write functions to caller object function. So i tried to do that with boost::function
here you have my code :
Server.cpp
#include "Server.h"
#include "Client.h"
#include "Network.h"
void Server::accept(void)
{
Network::ptr connection = Network::create(this->my_acceptor.get_io_service());
this->my_acceptor.async_accept(connection->getSocket(), bind(&Server::endCmd, this, connection, placeholders::error));
}
void Server::endCmd(Network::ptr connection, const boost::system::error_code& error)
{
if (!error)
{
std::cout << "Recu un client!" << std::endl;
this->client.push_back(new Client(connection));
this->client[client.size() - 1]->checkAuthentification();
std::cout << "Envoie du message de securisation" << std::endl;
std::cout << "Nouvelle ecoute" << std::endl;
this->accept();
}
}
Client.h and .cpp
#include "Network.h"
class Client
{
private:
Network::ptr connection;
public:
Client(Network::ptr);
~Client();
void checkAuthentification(void);
void endRead(const error_code& error, size_t nbytes);
void endWrite(const error_code &error);
};
#include "Client.h"
Client::Client(Network::ptr connect)
{
this->connection = connect;
this->connection->assignFunction(this);
}
Client::~Client()
{
this->connection->close();
}
void Client::checkAuthentification(void)
{
this->connection->Swrite("BIENVENUE", this);
}
void Client::endRead(const error_code& error, size_t nbytes)
{
if (!error && nbytes != 0)
{
std::cout << this->connection->getRcvMsg() << std::endl;
this->connection->Sread(this);
}
else
this->connection->close();
}
void Client::endWrite(const error_code &error)
{
if (!error)
this->connection->Sread(this);
else
this->connection->close();
}
and Network.cpp and .h
#include "Client.h"
class Network : public boost::enable_shared_from_this<Network>
{
private:
tcp::socket socket;
std::string rcv_msg;
std::string msg;
boost::array<char, 6> rbuffer;
boost::function<void (Client *, const error_code &, size_t)> fread;
boost::function<void (Client *, const error_code &)> fwrite;
public:
typedef boost::shared_ptr<Network> ptr;
Network(io_service &);
~Network();
void assignFunction(void);
void close(void);
void Sread(Client *cli);
void Swrite(std::string msg, Client *cli);
tcp::socket& getSocket(void);
std::string getRcvMsg(void);
static ptr create(io_service &);
};
#include "Client.h"
#include "Network.h"
Network::Network(io_service &ios) : socket(ios)
{
}
Network::~Network()
{
this->close();
}
void Network::assignFunction(void)
{
this->fread = &Client::endRead;
this->fwrite = &Client::endWrite;
}
void Network::close(void)
{
if (this->socket.is_open())
{
std::cout << "Connection closed" << std::endl;
this->socket.close();
}
}
void Network::Sread(Client *cli)
{
async_read(this->socket, buffer(this->rbuffer), bind(&Network::fread, cli, placeholders::error, placeholders::bytes_transferred));
}
void Network::Swrite(std::string msg, Client *cli)
{
this->msg = msg;
async_write(this->socket, buffer(this->msg, (int)this->msg.size()), bind(&Network::fwrite, cli, placeholders::error));
}
std::string Network::getRcvMsg(void)
{
return (std::string(this->rbuffer.c_array(), this->rbuffer.size()));
}
tcp::socket& Network::getSocket(void)
{
return (this->socket);
}
Network::ptr Network::create(io_service &ios)
{
return (ptr(new Network(ios)));
}
When i want to compile that, i have the following errors :
Client.h(10): error C2653: 'Network' : is not a class or namespace name
Client.h(10): error C2146: syntax error : missing ';' before identifier 'connection'
Client.h(10): error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
Client.h(10): error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
Client.h(13): error C2653: 'Network' : is not a class or namespace name
Client.h(13): error C2653: 'Network' : is not a class or namespace name
Client.h(13): error C2460: 'Client::ptr' : uses 'Client', which is being defined
Client.h(8) : see declaration of 'Client'
1>Client.cpp(6): error C2511: 'Client::Client(Network::ptr)' : overloaded member function not found in 'Client'
Client.h(8) : see declaration of 'Client'
1>Client.cpp(13): error C2039: 'connection' : is not a member of 'Client'
Client.h(8) : see declaration of 'Client'
1>Client.cpp(13): error C2227: left of '->close' must point to class/struct/union/generic type
1>Client.cpp(18): error C2039: 'connection' : is not a member of 'Client'
Client.h(8) : see declaration of 'Client'
1>Client.cpp(18): error C2227: left of '->Swrite' must point to class/struct/union/generic type
1>Client.cpp(25): error C2039: 'connection' : is not a member of 'Client'
Client.h(8) : see declaration of 'Client'
1>Client.cpp(25): error C2227: left of '->getRcvMsg' must point to class/struct/union/generic type
1>Client.cpp(26): error C2039: 'connection' : is not a member of 'Client'
Client.h(8) : see declaration of 'Client'
1>Client.cpp(26): error C2227: left of '->Sread' must point to class/struct/union/generic type
1>Client.cpp(29): error C2039: 'connection' : is not a member of 'Client'
Client.h(8) : see declaration of 'Client'
1>Client.cpp(29): error C2227: left of '->close' must point to class/struct/union/generic type
1>Client.cpp(35): error C2039: 'connection' : is not a member of 'Client'
Client.h(8) : see declaration of 'Client'
1>Client.cpp(35): error C2227: left of '->Sread' must point to class/struct/union/generic type
1>Client.cpp(37): error C2039: 'connection' : is not a member of 'Client'
Client.h(8) : see declaration of 'Client'
1>Client.cpp(37): error C2227: left of '->close' must point to class/struct/union/generic type
Commande.cpp
main.cpp
Client.h(10): error C2653: 'Network' : is not a class or namespace name
Client.h(10): error C2146: syntax error : missing ';' before identifier 'connection'
Client.h(10): error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
Client.h(10): error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
Client.h(13): error C2653: 'Network' : is not a class or namespace name
Client.h(13): error C2653: 'Network' : is not a class or namespace name
Client.h(13): error C2460: 'Client::ptr' : uses 'Client', which is being defined
Client.h(8) : see declaration of 'Client'
My_exception.cpp
Network.cpp
Network.h(14): error C2065: 'Client' : undeclared identifier
Network.h(14): error C2059: syntax error : ','
Network.h(15): error C2065: 'Client' : undeclared identifier
Network.h(15): error C2059: syntax error : ','
Network.h(25): error C2065: 'Client' : undeclared identifier
Network.h(25): error C2065: 'cli' : undeclared identifier
Network.h(25): error C2143: syntax error : missing ',' before ')'
Network.h(29): error C2143: syntax error : missing ';' before '}'
Client.h(8): error C2143: syntax error : missing ';' before '{'
Client.h(18): error C2143: syntax error : missing ';' before '}'
1>Network.cpp(6): error C2143: syntax error : missing ';' before '{'
1>Network.cpp(7): error C2143: syntax error : missing ';' before '}'
1>Network.cpp(10): error C2143: syntax error : missing ';' before '{'
1>Network.cpp(12): error C2143: syntax error : missing ';' before '}'
1>Network.cpp(15): error C2143: syntax error : missing ';' before '{'
1>Network.cpp(16): error C2653: 'Client' : is not a class or namespace name
1>Network.cpp(17): error C2653: 'Client' : is not a class or namespace name
1>Network.cpp(18): error C2143: syntax error : missing ';' before '}'
1>Network.cpp(21): error C2143: syntax error : missing ';' before '{'
1>Network.cpp(23): error C2143: syntax error : missing ';' before '{'
1>Network.cpp(26): error C2143: syntax error : missing ';' before '}'
1>Network.cpp(27): error C2143: syntax error : missing ';' before '}'
1>Network.cpp(30): error C2143: syntax error : missing ';' before '{'
1>Network.cpp(31): error C2355: 'this' : can only be referenced inside non-static member functions
1>Network.cpp(31): error C2227: left of '->rbuffer' must point to class/struct/union/generic type
1>Network.cpp(31): error C2065: 'cli' : undeclared identifier
1>Network.cpp(31): error C2143: syntax error : missing ',' before ')'
1>Network.cpp(32): error C2143: syntax error : missing ';' before '}'
1>Network.cpp(35): error C2065: 'Client' : undeclared identifier
1>Network.cpp(35): error C2065: 'cli' : undeclared identifier
1>Network.cpp(35): error C2143: syntax error : missing ',' before ')'
1>Network.cpp(36): error C2143: syntax error : missing ';' before '{'
1>Network.cpp(38): error C2355: 'this' : can only be referenced inside non-static member functions
1>Network.cpp(38): error C2227: left of '->msg' must point to class/struct/union/generic type
1>Network.cpp(38): error C2355: 'this' : can only be referenced inside non-static member functions
1>Network.cpp(38): error C2227: left of '->msg' must point to class/struct/union/generic type
1>Network.cpp(38): error C2228: left of '.size' must have class/struct/union
1>Network.cpp(38): error C2065: 'cli' : undeclared identifier
1>Network.cpp(38): error C2143: syntax error : missing ',' before ')'
1>Network.cpp(39): error C2143: syntax error : missing ';' before '}'
1>Network.cpp(42): error C2143: syntax error : missing ';' before '{'
1>Network.cpp(43): error C2355: 'this' : can only be referenced inside non-static member functions
1>Network.cpp(43): error C2227: left of '->rbuffer' must point to class/struct/union/generic type
1>Network.cpp(43): error C2228: left of '.size' must have class/struct/union
1>Network.cpp(43): error C2143: syntax error : missing ',' before ')'
1>Network.cpp(44): error C2143: syntax error : missing ';' before '}'
1>Network.cpp(47): error C2143: syntax error : missing ';' before '{'
1>Network.cpp(49): error C2143: syntax error : missing ';' before '}'
1>Network.cpp(52): error C2143: syntax error : missing ';' before '{'
1>Network.cpp(54): error C2143: syntax error : missing ';' before '}'
1>Network.cpp(55): error C2143: syntax error : missing ';' before '}'
1>Network.cpp(55): fatal error C1004: unexpected end-of-file found
Server.cpp
Client.h(10): error C2653: 'Network' : is not a class or namespace name
Client.h(10): error C2146: syntax error : missing ';' before identifier 'connection'
Client.h(10): error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
Client.h(10): error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
Client.h(13): error C2653: 'Network' : is not a class or namespace name
Client.h(13): error C2653: 'Network' : is not a class or namespace name
Client.h(13): error C2460: 'Client::ptr' : uses 'Client', which is being defined
Client.h(8) : see declaration of 'Client'
1>Server.cpp(22): error C2664: 'Client::Client(const Client &)' : cannot convert parameter 1 from 'Network::ptr' to 'const Client &'
Reason: cannot convert from 'Network::ptr' to 'const Client'
No user-defined-conversion operator available that can perform this conversion, or the operator cannot be called
After so many hours of "googlings" and reflexion, i didn't found why i have these errors.
Anyone can help me plz ?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
嗯..也许您遗漏了它们,但您未能包含标题:
编辑 这似乎很明显。如果不是那样的话
编辑 2 请注意,这些建议是在未给出有关包含结构的完整信息时键入的。现在问题已经解决了,但是每当编译器没有“看到”您的声明而出现奇怪的问题时,这些提示都是很好的一般故障排除建议:
检查头包含防护(如果它们不正确,例如
<前><代码>#ifndef __NETWORK_H
#定义__NETWORK_H
...
#endif // __NETWORK_H
在您的 Client.h...
检查命名空间问题
namespace { ... }
块中包含 Network.h检查预处理器问题 (
#define
造成严重破坏?)Server.cpp
Client.cpp
Network.cpp
可能(查看错误的形式):
Client.h
也是
Mmm.. Maybe you have left them out but you failed to include the headers:
Edit That seems to obvious. If it's not that then
Edit 2 Note that these suggestions have been typed when full information about the include structure wasn't given. The problems has been resolved now, but these hints are good general troubleshooting advice whenever strange issues arise with the compiler not 'seeing' your declarations:
check you header include guards (if they are incorrect, e.g.
in your Client.h...
check namespace issues
namespace { ... }
blockcheck preprocessor issues (
#define
s wreaking havoc?)Server.cpp
Client.cpp
Network.cpp
And probably (form looking at the errors):
Client.h
too
在 Network.h 中,不要包含 Client.h,而是包含 转发声明
类客户端
。In Network.h, do not include Client.h, but instead forward declare
class Client
.