我遇到了一个关于rxcpp
. 捕获rxcpp::connectable_observable
变量并调用它的连接方法时,出现如下编译错误。
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();
}
);
}
我尝试了 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();
}
);
}
您能告诉我为什么会出现编译错误以及如何解决吗?
谢谢!