0
votes

Hi I've a mimeType in ORTB, but couldn't pass compile, is there anyway to iterate banner.mimes and cast it to string?

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);
    }
}

it says: error: cannot bind âstd::basic_ostreamâ lvalue to âstd::basic_ostream&&â /usr/include/c++/4.6/ostream:581:5: error: initializing argument 1 of âstd::basic_ostream<_CharT, _Traits>& std::operator<<(std::basic_ostream<_CharT, _Traits>&&, const _Tp&) [with _CharT = char, _Traits = std::char_traits, _Tp = ORTB::MimeType]â

1
1. The if is completely redundant. 2. The error isn’t from the iteration, it’s from the cerr << … line. We need more context to be able to tell what’s causing this. But the definition of the << operator for MimeType seems to be wrong because somewhere the stream is being converted to a const&.Konrad Rudolph
I commented out the each line inside the for loop, but same error. ORTB::MimeType is from object Banner, I just need a way to get the object and convert it to a string.David Change

1 Answers

0
votes

On line 5 you're declaring a vector of mimes, so line 6 should be mimes.push_back (mime), or if you want the vector to contain strings you should change line 5 to std::vector<std::string>. Note that the call to mime.toString () requires toString be a member of ORTB::MimeType.

For the error involving the ostream operator (<<) you need to provide a way for the compiler to convert MimeType to something compatible with ostream. I'm going to assume that mime is an object of type ORTB::MimeType, but if it's not you just need to insert the appropriate class. There are two ways you can do this: Either a member function of ORTB::MimeType:

namespace ORTB {
    class MimeType {
    // Class definition...
    public:
    std::ostream& operator<< (std::ostream& stream) {
    // output MimeType's information here: return stream << Value1 << Value2...; 
    }
}

Or by creating a similar function at a more global scope if you don't have access to the definition of ORTB::MimeType:

    std::ostream& operator<< (std::ostream& stream, const MimeType& mt) {
    // output MimeType's information here: return stream << mt.Value1 << mt.Value2...; 
    }

Note that with option #2 you need public access to whatever you're outputting. Ideally your data would be specified as private, so you would have to use public getters.