- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我想创建一个异步。服务器。我成功地做到了这一点,但现在我想将 async_read/asyn_write 函数绑定(bind)到调用者对象函数。所以我试着用 boost::function 来做到这一点这里有我的代码:
服务器.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
经过这么多小时的“谷歌搜索”和反射(reflection),我没有找到为什么会出现这些错误。任何人都可以帮助我吗?
最佳答案
嗯..也许你遗漏了它们但是你没有包含标题:
编辑 这似乎是显而易见的。如果不是那样的话
编辑 2 请注意,这些建议是在未提供有关包含结构的完整信息时输入的。问题现在已经解决,但是当编译器没有“看到”您的声明时出现奇怪的问题时,这些提示是很好的一般故障排除建议:
检查您的 header 包含守卫(如果它们不正确,例如
#ifndef __NETWORK_H
#define __NETWORK_H
...
#endif // __NETWORK_H
在您的 Client.h 中...
检查命名空间问题
namespace { ... }
block 中包含 Network.h检查预处理器问题(#define
造成严重破坏?)
服务器.cpp
include "Network.h"
include "Server.h"
//....
客户端.cpp
include "Network.h"
include "Server.h"
网络.cpp
include "Network.h"
可能(查看错误的形式):
客户端.h
include "Network.h"
也是
关于c++ - 使用异步函数提升 asio 编译错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8158996/
我已经使用 vue-cli 两个星期了,直到今天一切正常。我在本地建立这个项目。 https://drive.google.com/open?id=0BwGw1zyyKjW7S3RYWXRaX24tQ
您好,我正在尝试使用 python 库 pytesseract 从图像中提取文本。请找到代码: from PIL import Image from pytesseract import image_
我的错误 /usr/bin/ld: errno: TLS definition in /lib/libc.so.6 section .tbss mismatches non-TLS reference
我已经训练了一个模型,我正在尝试使用 predict函数但它返回以下错误。 Error in contrasts<-(*tmp*, value = contr.funs[1 + isOF[nn]])
根据Microsoft DataConnectors的信息我想通过 this ODBC driver 创建一个从 PowerBi 到 PostgreSQL 的连接器使用直接查询。我重用了 Micros
我已经为 SoundManagement 创建了一个包,其中有一个扩展 MediaPlayer 的类。我希望全局控制这个变量。这是我的代码: package soundmanagement; impo
我在Heroku上部署了一个应用程序。我正在使用免费服务。 我经常收到以下错误消息。 PG::Error: ERROR: out of memory 如果刷新浏览器,就可以了。但是随后,它又随机发生
我正在运行 LAMP 服务器,这个 .htaccess 给我一个 500 错误。其作用是过滤关键字并重定向到相应的域名。 Options +FollowSymLinks RewriteEngine
我有两个驱动器 A 和 B。使用 python 脚本,我在“A”驱动器中创建一些文件,并运行 powerscript,该脚本以 1 秒的间隔将驱动器 A 中的所有文件复制到驱动器 B。 我在 powe
下面的函数一直返回这个错误信息。我认为可能是 double_precision 字段类型导致了这种情况,我尝试使用 CAST,但要么不是这样,要么我没有做对...帮助? 这是错误: ERROR: i
这个问题已经有答案了: Syntax error due to using a reserved word as a table or column name in MySQL (1 个回答) 已关闭
我的数据库有这个小问题。 我创建了一个表“articoli”,其中包含商品的品牌、型号和价格。 每篇文章都由一个 id (ID_ARTICOLO)` 定义,它是一个自动递增字段。 好吧,现在当我尝试插
我是新来的。我目前正在 DeVry 在线学习中级 C++ 编程。我们正在使用 C++ Primer Plus 这本书,到目前为止我一直做得很好。我的老师最近向我们扔了一个曲线球。我目前的任务是这样的:
这个问题在这里已经有了答案: What is an undefined reference/unresolved external symbol error and how do I fix it?
我的网站中有一段代码有问题;此错误仅发生在 Internet Explorer 7 中。 我没有在这里发布我所有的 HTML/CSS 标记,而是发布了网站的一个版本 here . 如您所见,我在列中有
如果尝试在 USB 设备上构建 node.js 应用程序时在我的树莓派上使用 npm 时遇到一些问题。 package.json 看起来像这样: { "name" : "node-todo",
在 Python 中,您有 None单例,在某些情况下表现得很奇怪: >>> a = None >>> type(a) >>> isinstance(a,None) Traceback (most
这是我的 build.gradle (Module:app) 文件: apply plugin: 'com.android.application' android { compileSdkV
我是 android 的新手,我的项目刚才编译和运行正常,但在我尝试实现抽屉导航后,它给了我这个错误 FAILURE: Build failed with an exception. What wen
谁能解释一下?我想我正在做一些非常愚蠢的事情,并且急切地等待着启蒙。 我得到这个输出: phpversion() == 7.2.25-1+0~20191128.32+debian8~1.gbp108
我是一名优秀的程序员,十分优秀!