Boost compile error on converting UUID to string using boost::lexical_cast

≡放荡痞女 提交于 2020-01-03 13:35:10

问题


I have this code which is based on several posts in SO:

boost::uuids::uuid uuid = boost::uuids::random_generator()();
auto uuidString= boost::lexical_cast<std::string>(uuid);

but when I am compiling this code, I am getting this error:

Source type is neither std::ostream`able nor std::wostream`able     C:\Local\boost\boost\lexical_cast\detail\converter_lexical.hpp  

How can I fix this error?


回答1:


You're missing the include, I guess:

Live On Coliru

#include <boost/lexical_cast.hpp>
#include <boost/uuid/uuid_io.hpp>
#include <boost/uuid/uuid.hpp>
#include <boost/uuid/random_generator.hpp>

int main() {
    boost::uuids::uuid uuid = boost::uuids::random_generator()();
    auto uuidString = boost::lexical_cast<std::string>(uuid);
}



回答2:


You could try:

std::stringstream ss;
std::string       uuidStr;

boost::uuids::uuid uuid = boost::uuids::random_generator()();

ss << uuid;
ss >> uuidStr;

The Documentation states:

Stream Operators

The standard input and output stream operators << and >> are provided by including boost/uuid/uuid_io.hpp. The string representation of a uuid is hhhhhhhh-hhhh-hhhh-hhhh-hhhhhhhhhhhh where h is a hexidecimal digit.

boost::uuids::uuid u1; // initialize uuid

std::stringstream ss;
ss << u1;

boost::uuids::uuid u2;
ss >> u2;

assert(u1, u2);

However the lexical_cast should work as well.

Maybe you should check what uuid actually contains to figure out if there's something wrong with the generated uuid.

Also:

boost::uuids::uuid u; // initialize uuid

std::string s1 = to_string(u);

should be slightly faster according to the doc.



来源:https://stackoverflow.com/questions/29631039/boost-compile-error-on-converting-uuid-to-string-using-boostlexical-cast

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!