CPP 反应式扩展中的编译错误



我遇到了一个关于rxcpp的奇怪问题。捕获rxcpp::connectable_observable变量并调用其连接方法时,我收到如下编译错误。

1>------ Build started: Project: LearnRx, Configuration: Debug x64 ------
1>Main.cpp
1>c:usersliuzichaosourcereposlearnrxlearnrxmain.cpp(19): warning C4477: 'printf' : format string '%d' requires an argument of type 'int', but variadic argument 1 has type 'std::thread::id'
1>c:usersliuzichaosourcereposlearnrxlearnrxmain.cpp(259): error C2662: 'rxcpp::composite_subscription rxcpp::connectable_observable<SourceValue,Multicast>::connect(rxcpp::composite_subscription)': cannot convert 'this' pointer from 'const Result' to 'rxcpp::connectable_observable<SourceValue,Multicast> &'
1>        with
1>        [
1>            SourceValue=int,
1>            Multicast=rxcpp::operators::detail::multicast<int,rxcpp::observable<int,rxcpp::sources::detail::create<int,publish_example::<lambda_3a462b77ca22ce68ef79403bfe94c65d>>>,rxcpp::subjects::subject<int>>
1>        ]
1>        and
1>        [
1>            Result=rxcpp::connectable_observable<int,rxcpp::operators::detail::multicast<int,rxcpp::observable<int,rxcpp::sources::detail::create<int,publish_example::<lambda_3a462b77ca22ce68ef79403bfe94c65d>>>,rxcpp::subjects::subject<int>>>
1>        ]
1>        and
1>        [
1>            SourceValue=int,
1>            Multicast=rxcpp::operators::detail::multicast<int,rxcpp::observable<int,rxcpp::sources::detail::create<int,publish_example::<lambda_3a462b77ca22ce68ef79403bfe94c65d>>>,rxcpp::subjects::subject<int>>
1>        ]
1>c:usersliuzichaosourcereposlearnrxlearnrxmain.cpp(259): note: Conversion loses qualifiers
1>c:usersliuzichaosourcereposlearnrxlearnrxmain.cpp(334): warning C4477: 'printf' : format string '%d' requires an argument of type 'int', but variadic argument 1 has type 'std::thread::id'
1>c:usersliuzichaosourcereposlearnrxlearnrxmain.cpp(338): warning C4477: 'printf' : format string '%d' requires an argument of type 'int', but variadic argument 1 has type 'std::thread::id'
1>c:usersliuzichaosourcereposlearnrxlearnrxmain.cpp(345): warning C4477: 'printf' : format string '%d' requires an argument of type 'int', but variadic argument 1 has type 'std::thread::id'
1>c:usersliuzichaosourcereposlearnrxlearnrxmain.cpp(346): warning C4477: 'printf' : format string '%d' requires an argument of type 'int', but variadic argument 1 has type 'std::thread::id'
1>c:usersliuzichaosourcereposlearnrxlearnrxmain.cpp(348): warning C4477: 'printf' : format string '%d' requires an argument of type 'int', but variadic argument 1 has type 'std::thread::id'
1>Done building project "LearnRx.vcxproj" -- FAILED.
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========

问题代码如下所示。

void publish_example() {
    auto o = rxcpp::observable<>::create<int>([](rxcpp::subscriber<int> s) {
        s.on_next(0);
        s.on_next(1);
    }).publish();
    o.subscribe([](int v) {printf("%d", v); });
    rxcpp::observable<>::timer(std::chrono::microseconds(2000)).subscribe(
        [](const int i) {},
        [](const std::exception_ptr& e) {},
        [o]() {
        o.connect();
    }
    );
}

我尝试了 2 种方法通过编译
1. 注释 "o.connect()"
1. 将变量 O 转换为静态变量,这样 O 就不需要被捕获了。代码如下所示

void publish_example() {
    static auto o = rxcpp::observable<>::create<int>([](rxcpp::subscriber<int> s) {
        s.on_next(0);
        s.on_next(1);
    }).publish();
    o.subscribe([](int v) {printf("%d", v); });
    rxcpp::observable<>::timer(std::chrono::microseconds(2000)).subscribe(
        [](const int i) {},
        [](const std::exception_ptr& e) {},
        []() {
        o.connect();
    }
    );
}

你能帮忙告诉我为什么会出现编译错误并弄清楚吗?

谢谢!

我想通了。

这里发生的事情是因为 connectable_observable::connect 不是一个 const 方法。将其更改为 const 方法后,编译错误消失了。

相关内容

  • 没有找到相关文章