错误:“boost::array”不是一种类型

error: ‘boost::array’ is not a type

本文关键字:一种 类型 boost array 错误      更新时间:2023-10-16

我创建了一个简单的类,如下所示。我在这里尝试做的是将 boost::array 转换为字符串,以便可以打印出来或用于其他目的。

#include <iostream>                                                                                                                                                                                                
#include <exception>
#include <boost/array.hpp>
#include <boost/asio.hpp>
#include <boost/shared_ptr.hpp>
#include <boost/enable_shared_from_this.hpp>
#include <boost/algorithm/hex.hpp>
#include <boost/asio.hpp>
#include <boost/bind.hpp>
#include <algorithm>
#include <sstream>
#include <iomanip>

class buffer_manager
{
  public:
    buffer_manager()
    {   
    }   
    ~buffer_manager()
    {   
    }   
    std::string message_buffer(boost::array buf)
    {   
        message = boost::algorithm::hex(recv_buf.begin(), recv_buf.end(), back_inserter(result));
        return message;
    }   
private:
  boost::array<unsigned char, 4096> recv_buf = {{0}};
  std::string message;
};

在编译时,我似乎遇到了一个奇怪的错误。

g++ -std=c++11 -g -Wall -pedantic -c buffer_manager.cpp -o buffer_manager.o
buffer_manager.cpp:26:39: error: ‘boost::array’ is not a type
     std::string message_buffer(boost::array buf)
                                       ^
buffer_manager.cpp: In member function ‘std::string buffer_manager::message_buffer(int)’:
buffer_manager.cpp:28:89: error: ‘result’ was not declared in this scope
         message = boost::algorithm::hex(recv_buf.begin(), recv_buf.end(), back_inserter(result));
                                                                                         ^
buffer_manager.cpp:28:95: error: ‘back_inserter’ was not declared in this scope
         message = boost::algorithm::hex(recv_buf.begin(), recv_buf.end(), back_inserter(result));
                                                                                               ^
buffer_manager.cpp:28:95: note: suggested alternative:
In file included from /usr/include/c++/4.9/bits/stl_algobase.h:67:0,
                 from /usr/include/c++/4.9/bits/char_traits.h:39,
                 from /usr/include/c++/4.9/ios:40,
                 from /usr/include/c++/4.9/ostream:38,
                 from /usr/include/c++/4.9/iostream:39,
                 from buffer_manager.cpp:1:
/usr/include/c++/4.9/bits/stl_iterator.h:480:5: note:   ‘std::back_inserter’
     back_inserter(_Container& __x)
     ^
Makefile:15: recipe for target 'buffer_manager.o' failed

由于 boost::array 是一种模板类型,因此当您使用 boost::array 作为函数的参数时,您需要有一个模板参数。我建议使用typedef:

typedef boost::array<unsigned char, 4096> My_Array_Type;
//...
std::string message_buffer(My_Array_Type& buf);
//...
My_Array_Type rec_buf;

编辑 1:
在您的情况下,您可能会发现std::vector更好的选择。 此外,typedef在这里也很方便。