错误:'boost :: array'不是一个类型

时间:2016-02-21 19:38:39

标签: c++ arrays c++11 boost

我创建了一个简单的类,如下所示。我在这里尝试做的是将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

1 个答案:

答案 0 :(得分:3)

由于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在这里也很方便。