Increase error compilation when converting UUID to string using boost :: lexical_cast

I have this code that 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 compile this code, I get 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?

+4
source share
2 answers

You are missing the include, I think:

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);
}
+6
source

You can try:

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

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

ss << uuid;
ss >> uuidStr;

The documentation reads:

Stream operators

The standard input and output stream operators <and → are provided by enabling boost / uuid / uuid_io.hpp. The uuid string representation is hhhhhhhh-hhhh-hhhh-hhhh-hhhhhhhhhhhh, where h is a hexadecimal digit.

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

std::stringstream ss;
ss << u1;

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

assert(u1, u2);

However, it lexical_castshould work.

, , uuid, , - uuid.

:

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

std::string s1 = to_string(u);

.

+2

All Articles