9445a4dcb9
9445a4dcb9

Reputation: 11

C2039 C2228 'get_value_or': is not a member of 'network::basic_string_view<char,std::char_traits<char>>'

I am trying to compile a piece of code but receive the following errors:

error C2039: 'get_value_or': is not a member of 'network::basic_string_view<char,std::char_traits<char>>'
note: see declaration of 'network::basic_string_view<char,std::char_traits<char>>'

error C2228: left of '.get_value_or' must have class/struct/union
#include "StdInc.h"
#include <boost/utility/string_ref.hpp>

#include <C:\Users\USER\PROJECT\network\uri\uri.hpp>
#include <C:\Users\USER\PROJECT\network\string_view.hpp>


boost::string_ref scheme = serviceUri.scheme().get_value_or("part1");

if (scheme == "part1")
{
    boost::string_ref hostname = serviceUri.host().get_value_or("localhost");
    int port = serviceUri.port<int>().get_value_or(2800);

I am using Visual Basic 2015 Update 3 with Boost 1.57.0

Upvotes: 0

Views: 255

Answers (1)

sehe
sehe

Reputation: 393114

You were using cppnetlib/uri.

However, 4 years ago they had (another) breaking interface change:

Replaced the accessors that return optional string_view objects with two separate functions.

What's worse, they still return a hand-rolled string_view instead of the standard one.

Also, their version of network::optional<> never had a get_value_or. In fact get_value_or is Boost-only, where it is deprecated in favor of the (standard) value_or.

Conclusion

Use the has_scheme() accessor to see whether a scheme is present. You could make an optional out of that:

#include <boost/utility/string_ref.hpp>
#include <network/uri/uri.hpp>
#include <network/string_view.hpp>
#include <optional>

int main() {
    network::uri serviceUri("http://cpp-netlib.org/");

    network::optional<network::string_view> scheme, hostname, port;

    if (serviceUri.has_scheme())
        scheme = serviceUri.scheme();
    if (serviceUri.has_host())
        hostname = serviceUri.host();
    if (serviceUri.has_port())
        port = serviceUri.port();

    scheme   = scheme.value_or("part1");
    hostname = hostname.value_or("localhost");
    port     = scheme.value_or("2800");
}

Alternatively, you could steer clear of network::optional and network::string_view alltogether, and simply write:

#include <network/uri/uri.hpp>
#include <optional>

int main() {
    network::uri serviceUri("http://cpp-netlib.org/");

    std::string const scheme = serviceUri.has_scheme()
        ?  serviceUri.scheme().to_string()
        : "part1";

    std::string const host = serviceUri.has_host()
        ?  serviceUri.host().to_string()
        : "localhost";

    std::string const port = serviceUri.has_port()
        ?  serviceUri.port().to_string()
        : "2800";
}

Upvotes: 0

Related Questions