反应性扩展中的cpp编译错误

时间:2019-02-01 02:39:30

标签: c++11 compiler-errors rxcpp

我遇到一个关于rxcpp的奇怪问题。 捕获rxcpp::connectable_observable变量并调用它的connect方法时,出现如下编译错误。

1>------ Build started: Project: LearnRx, Configuration: Debug x64 ------
1>Main.cpp
1>c:\users\liuzichao\source\repos\learnrx\learnrx\main.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:\users\liuzichao\source\repos\learnrx\learnrx\main.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:\users\liuzichao\source\repos\learnrx\learnrx\main.cpp(259): note: Conversion loses qualifiers
1>c:\users\liuzichao\source\repos\learnrx\learnrx\main.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:\users\liuzichao\source\repos\learnrx\learnrx\main.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:\users\liuzichao\source\repos\learnrx\learnrx\main.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:\users\liuzichao\source\repos\learnrx\learnrx\main.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:\users\liuzichao\source\repos\learnrx\learnrx\main.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();
    }
    );
}

我尝试了两种方法来通过编译
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();
    }
    );
}

您能帮我告诉我为什么会出现编译错误并如何解决吗?

谢谢!

1 个答案:

答案 0 :(得分:0)

我知道了。

此处发生的原因是因为connectable_observable :: connect不是const方法。将其更改为const方法后,编译错误消失了。