我在嘗試,如果我收到任何數據功能boost::asio::async_read
的返回值轉換爲int
爲了看:獲得通過的boost :: ASIO :: async_read讀取的字節數
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基洛夫我不知道任何時候我雙擊這個錯誤就放在這裏bind.hpp'模板結構result_traits <不明,F> { 的typedef typename的˚F:: result_type的類型; };'但是我不是用F IDK的什麼是錯的!? –
Abanoub
2011-05-20 21:25:21
對不起,我幾乎立即刪除我的意見,因爲我錯過了一些東西。對不起,不能幫助你,我從來沒有使用過助力,不幸的是。 – 2011-05-20 21:30:23