如何迭代类型:mimeType
How to Iterate to type: mimeType
嗨,我在 ORTB 中有一个 mimeType,但无法通过编译,无论如何可以迭代 banner.mimes 并将其转换为字符串吗?
cerr << "banner.mimes:" << banner.mimes.empty() << endl;
if (!banner.mimes.empty()){
for(auto & mime : banner.mimes) {
cerr << "banner.mime:" << mime << endl;
std::vector<ORTB::MimeType> mimes;
mimes.push_back(mime.toString());
ctx.br->segments.add("mimes", mimes);
}
}
它说:错误:无法将 âstd::basic_ostreamâ lvalue 绑定到 âstd::basic_ostream&&â/usr/include/c++/4.6/ostream:581:5:错误:初始化参数 1 的 âstd::basic_ostream<_CharT, _Traits>& std::operator<<(std::basic_ostream<_CharT, _Traits>&&, const _Tp&) [其中 _CharT = char, _Traits = std::char_traits, _Tp = ORTB::MimeType]â
在第 5 行,您声明的是 mime 向量,因此第 6 行应该是mimes.push_back (mime)
的,或者如果您希望向量包含字符串,则应将第 5 行更改为 std::vector<std::string>
。请注意,对mime.toString ()
的调用要求 toString 是 ORTB::MimeType 的成员。
对于涉及 ostream 运算符 (<<) 的错误,您需要为编译器提供一种将 MimeType 转换为与 ostream 兼容的方法。我将假设mime
是 ORTB::MimeType
类型的对象,但如果不是,您只需要插入适当的类。有两种方法可以执行此操作:ORTB::MimeType
的成员函数之一:
namespace ORTB {
class MimeType {
// Class definition...
public:
std::ostream& operator<< (std::ostream& stream) {
// output MimeType's information here: return stream << Value1 << Value2...;
}
}
或者,如果您无权访问ORTB::MimeType
的定义,则通过在更全局的范围内创建类似的函数:
std::ostream& operator<< (std::ostream& stream, const MimeType& mt) {
// output MimeType's information here: return stream << mt.Value1 << mt.Value2...;
}
请注意,使用选项 #2,您需要对输出的任何内容进行公共访问。理想情况下,您的数据将被指定为私有数据,因此您必须使用公共获取者。
- ArduinoJson 6.15.2:JsonObject没有命名类型
- 防止主数据类型C++的隐式转换
- 大量序列中核苷酸类型的快速计数
- 如何从C++中的依赖类型中获得它所依赖的类型
- 有关插入适配器的错误。[错误]请求从 'back_insert_iterator<vector<>>' 类型转换为非标量类型
- 是否可以初始化不可复制类型的成员变量(或基类)
- 如何获取std::result_of函数的返回类型
- 从父命名空间重载类型
- 如果C++类在类方法中具有动态分配,但没有构造函数/析构函数或任何非静态成员,那么它仍然是POD类型吗
- 我想将一个对T类型的非常量左值引用绑定到一个T类型的临时值
- Openssl 1.1.1d无效使用不完整的类型"struct dsa_st"
- 访问者访问变体并返回不同类型时出错
- 在VS2010-VS2015下编译时,如何使用decltype作为较大类型表达式的LHS
- 处理小于cpu数据总线的数据类型.(c++转换为机器代码)
- C++ 雷神库 - 使用资源加载器类时出现问题(不命名类型)
- 模板元程序查找相似的连续类型名称
- 是否可以从int转换为enum类类型
- 构造函数正在调用一个使用当前类类型的函数
- 我应该使用什么来代替void作为变体中的替代类型之一
- 如何迭代类型:mimeType