如何迭代类型:mimeType

How to Iterate to type: mimeType

本文关键字:类型 mimeType 迭代 何迭代      更新时间:2023-10-16

嗨,我在 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 兼容的方法。我将假设mimeORTB::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,您需要对输出的任何内容进行公共访问。理想情况下,您的数据将被指定为私有数据,因此您必须使用公共获取者。