错误:“boost::array”不是一种类型
error: ‘boost::array’ is not a type
我创建了一个简单的类,如下所示。我在这里尝试做的是将 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
在这里也很方便。
相关文章:
- 在运行时检查继承是否只有一种类型和 void*
- 另一种类型的智能ptr,比如具有弱refs的unique_ptr
- void* 数组将元素转换为另一种类型
- 将一种类型的比特重新解释为不同类型的比特的技术
- 为什么需要类型名称,即使似乎足以推断名称应该是一种类型?
- 在 c++ 中将一种结构类型分配给另一种类型
- 将空基类优化对象强制转换为另一种类型是否会破坏严格的别名?
- 我怎样才能让编译器推导出一种类型的 nullptr
- 一种类型的多个 cv 分解
- 如何将对象定义为一种类型,然后再将其声明为子类型
- 类中的结构不是一种类型
- 一种类型特征,标识哪个类提供通过重载解析选择的函数
- 将 32 位浮点数和不强制转换的 32 位整数与双精度进行比较,当其中一个值可能太大而无法完全适合另一种类型时
- constexpr检查一种类型
- 何时与另一种类型兼容
- 依赖名称不是一种类型,带有 SFINAE 的模板
- 自动将一种 c++ 类型转换为另一种
- C 模板可以匹配(仅)一种类型的列表或另一种类型的列表
- 是否可以将一种类型的数组转换为大小不同的另一种类型的数组?
- 在比较中使用已强制转换为另一种类型的函数指针是否定义良好?