C++ 当使用boost::asio read_some()函数时,boost::array在read_some()函数调用时崩溃

C++ 当使用boost::asio read_some()函数时,boost::array在read_some()函数调用时崩溃,c++,arrays,boost,std,C++,Arrays,Boost,Std,我正在使用boost的套接字和字符数组读取套接字的结果。根据我在网上看到的例子,包括Boost文档,我有以下几点: #include <boost/asio.hpp> #include <array> #include <string> #define MAXSIZE 1000000 //... void MyClass::processCommand(std::string command) { boost::asio::io_service io;

我正在使用boost的套接字和字符数组读取套接字的结果。根据我在网上看到的例子,包括Boost文档,我有以下几点:

#include <boost/asio.hpp>
#include <array>
#include <string>
#define MAXSIZE 1000000
//...
void MyClass::processCommand(std::string command)
{
  boost::asio::io_service io;
  boost::asio::ip::tcp::socket socket(io);
  boost::asio::ip::tcp::endpoint e(boost::asio::ip::address::from_string("127.0.0.1"), 60151);  //Info for the connection I need to make...
  this->socket.open(boost::asio::ip::tcp::v4());
  this->socket.connect(e);
  this->socket.write_some(boost::asio::buffer(command, command.size());
  this->socket.send(boost::asio::buffer(command, command.size());

  std::array<char, MAXSIZE> buffer;
  this->socket.read_some(boost::asio::buffer(buffer));
}
根据我所看到的例子,这似乎应该是可行的。不过我有个问题;无论出于何种原因,我在Qt5.2中使用的编译器似乎拒绝将std::array识别为有效。即使使用include,也尝试将缓冲区声明为std::tr1::array buffer;。同样按照同样的逻辑,我尝试使用名称空间std::tr1添加;然后将缓冲区定义为数组缓冲区;。这两个都没有被编辑;由于参数类型不匹配,在调用read_some的行中都出现了错误

我确实想到了这一点,这是可能的,因为那些看起来应该有用的东西在读的时候往往会僵住,阅读需要很长时间,而且只是超时了?如果是这样的话,我可以解决这个问题,我只是不知道如何判断这是怎么回事还是崩溃了。

您可以使用std::array boost::array std::vector和std::string,如下所示:


应该没问题,看到了吗

如果没有C++11,请将boost::array与boost/array.hpp一起使用


在所有其他情况下,请查看您的编译器和Boost问题跟踪器中可能出现的不兼容报告

您必须使用c++11类似-std=c++1的东西来包含@juanchopanza感谢您的响应,我尝试使用Boost::array而不是std::array,但它仍然在同一位置崩溃:/What can crash?您声称它没有编译。@juanchopanza很抱歉,如果这是含糊不清的,我的意思是boost::array代码与buffer[MAXSIZE]=的代码在同一点崩溃。它确实不像std::array那样编译,但请不要编辑你的问题,一旦回答了,就把它变成一个新问题。感谢你的回答,我尝试使用boost::array而不是std::array,后者似乎与页面上的重载11相匹配,但尽管它编译时在读取某行崩溃,你知道为什么会这样吗?不幸的是,我无法让std::array重载正常工作,它仍然告诉我array不是std的成员:/好吧,这是另一个问题,继续在它自己的线程中问它,尽可能多的细节可以帮助了解发生了什么样的崩溃。我刚刚检查了,我们的项目使用了VS2008,所以我似乎不能使用C++11。但是,当我为boost/array.hpp添加include指令并将std::array更改为boost::array时,它会编译,但在调用read_some的行中崩溃。你知道是什么导致了这个问题吗?
#include <boost/asio.hpp>
#include <array>
#include <string>
#define MAXSIZE 1000000
//...
void processCommand(std::string command)
{
    boost::asio::io_service io;
    boost::asio::ip::tcp::socket socket(io);
    boost::asio::ip::tcp::endpoint e(boost::asio::ip::address::from_string("127.0.0.1"), 60151);  //Info for the connection I need to make...

    socket.open(boost::asio::ip::tcp::v4());
    socket.connect(e);
    socket.write_some(boost::asio::buffer(command));
    socket.send(boost::asio::buffer(command));

    std::array<char, MAXSIZE> buf;
    socket.read_some(boost::asio::buffer(buf));
}

int main()
{
    processCommand("EHLO");
}