【发布时间】:2011-05-20 21:19:31
【问题描述】:
我正在尝试将函数 boost::asio::async_read 的返回值转换为 int 以查看我是否收到任何数据:
int recvlen = boost::asio::async_read (
socket_,
boost::asio::buffer((char*)buffer, 1000),
boost::bind(&Connection::Receive, this, boost::asio::placeholders::error)
);
这是我其余代码的上下文中的该语句,它不会编译:
.h 文件:
#ifndef _CONNECTION_H_
#define _CONNECTION_H_
#include <boost/bind.hpp>
#include <boost/shared_ptr.hpp>
#include <boost/enable_shared_from_this.hpp>
#include <boost/asio.hpp>
#include <stdint.h>
class Connection
{
public:
Connection(boost::asio::io_service& io_service);
~Connection();
boost::asio::ip::tcp::socket& socket() {return socket_;}
void Send(uint8_t* buffer, int length);
bool Receive();
protected:
virtual void OnReceived(uint8_t* buffer, int len) = 0;
private:
boost::asio::ip::tcp::socket socket_;
};
#endif
.cpp 文件:
#include "Connection.h"
Connection::Connection(boost::asio::io_service& io_service)
: socket_(io_service) {}
Connection::~Connection() {}
void Connection::Send(uint8_t* buffer,int length) {
boost::asio::async_write (
socket_,
boost::asio::buffer(buffer, length),
boost::bind(&Connection::Send, this,boost::asio::placeholders::error)
);
}
bool Connection::Receive(){
uint8_t* buffer = new uint8_t[1000];
//Conversion excerpt
int recvlen = boost::asio::async_read (
socket_,
boost::asio::buffer((char*)buffer, 1000),
boost::bind(&Connection::Receive, this, boost::asio::placeholders::error)
);
if (recvlen <= 0) {
delete[] buffer;
return false;
}
this->OnReceived(buffer, recvlen);
delete[] buffer;
return true;
}
这些是这段代码在 Visual C++ 中产生的错误:
error C2825: 'F': must be a class or namespace when followed by '::' e:\boost_1_46_1\boost_1_46_1\boost\bind\bind.hpp 69
error C2039: 'result_type' : is not a member of '`global namespace'' e:\boost_1_46_1\boost_1_46_1\boost\bind\bind.hpp 69
error C2146: syntax error : missing ';' before identifier 'type' e:\boost_1_46_1\boost_1_46_1\boost\bind\bind.hpp 69
error C2208: 'boost::_bi::type' : no members defined using this type e:\boost_1_46_1\boost_1_46_1\boost\bind\bind.hpp 69
error C1903: unable to recover from previous error(s); stopping compilation e:\boost_1_46_1\boost_1_46_1\boost\bind\bind.hpp 69
IntelliSense: a value of type "void" cannot be used to initialize an entity of type "int" d:\c++\ugs\common\connection.cpp 18
IntelliSense: the #endif for this directive is missing d:\c++\ugs\common\connection.h 1
IntelliSense: this declaration has no storage class or type specifier d:\c++\ugs\common\connection.h 26
我怎样才能完成我想做的事情?另外,这些错误是什么意思,我该如何解决?
【问题讨论】:
-
@Kiril Kirov 我也不知道当我双击这个错误时它会出现在这里 bind.hpp
template<class F> struct result_traits<unspecified, F> { typedef typename F::result_type type; };但我没有使用 F idk 有什么问题!!? -
对不起,我几乎立即删除了我的评论,因为我错过了一些东西。抱歉,帮不了你,很遗憾,我从未使用过 boost。
标签: c++ sockets visual-c++ boost boost-asio