C2039 C2228'get_value_or':不是'network :: basic_string_view >'

问题描述 投票:-1回答:1

我正在尝试编译一段代码,但收到以下错误:

错误C2039:'get_value_or':不是'network :: basic_string_view >'的成员注意:请参见“ network :: basic_string_view >”的声明错误C2228:“。get_value_or”的左侧必须具有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);

我正在将Visual Basic 2015 Update 3与Boost 1.57.0一起使用

c++ boost visual-studio-2015 uri
1个回答
0
投票

您正在使用cppnetlib/uri

但是,4年前,他们有(另一个)breaking interface change

用两个单独的函数替换了返回可选的string_view对象的访问器。

更糟糕的是,他们仍然返回手动滚动的string_view,而不是标准的滚动。

而且,他们的network::optional<>版本从来没有get_value_or。实际上,get_value_or仅是Boost的,where it is deprecated赞成(标准)value_or

结论

使用has_scheme()访问器查看是否存在方案。您可以在其中进行选择:

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

或者,您可以完全避开network::optionalnetwork::string_view,只需写:

#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";
}
© www.soinside.com 2019 - 2024. All rights reserved.