繁体   English   中英

C2039 C2228 'get_value_or': 不是 'network::basic_string_view 的成员<char,std::char_traits<char> &gt;' </char,std::char_traits<char>

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

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

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

我正在使用带有 Boost 1.57.0 的 Visual Basic 2015 Update 3

您正在使用cppnetlib/uri

然而,4 年前他们有(另一个) 突破性的界面变化

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

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

此外,他们的network::optional<>版本从来没有get_value_or 事实上get_value_or是 Boost-only 的, 它被弃用,取而代之的是(标准) 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";
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM