我想要将数组值解压缩到不同的类变量中,但是我得到了一个错误。
auto [SendLowROS.motorCmd[FR_0].Kp, SendLowROS.motorCmd[FR_1].Kp, SendLowROS.motorCmd[FR_2].Kp,
SendLowROS.motorCmd[FL_0].Kp, SendLowROS.motorCmd[FL_1].Kp, SendLowROS.motorCmd[FL_2].Kp,
SendLowROS.motorCmd[RR_0].Kp, SendLowROS.motorCmd[RR_1].Kp, SendLowROS.motorCmd[RR_2].Kp,
SendLowROS.motorCmd[RL_0].Kp, SendLowROS.motorCmd[RL_1].Kp, SendLowROS.motorCmd[RL_2].Kp] = msg.Kp;/home/src/llm.cpp: In member function ‘void Driver::jointCommandCallback(msgs::JointCMD)’:
/home/src/llm.cpp:65:25: error: expected ‘]’ before ‘.’ token
auto [SendLowROS.motorCmd[FR_0].Kp, SendLowROS.motorCmd[FR_1].Kp, SendLowROS.motorCmd[FR_2].Kp,
^
/home/src/mbs_unitree_ros/src/llm.cpp:68:111: error: ‘std::_Vector_base<float, std::allocator<float> >’ is an inaccessible base of ‘std::vector<float>’
SendLowROS.motorCmd[RL_0].Kp, SendLowROS.motorCmd[RL_1].Kp, SendLowROS.motorCmd[RL_2].Kp] = msg.Kp;发布于 2020-12-09 18:51:54
你不能这么做。
结构化绑定只是为您要解包的任何部分创建新的唯一名称。
例如:
auto&& [a,b,c] = std::tuple{1,2,3};
some_class.a = a;
some_class.b = b;
some_class.c = c;详情请参见https://en.cppreference.com/w/cpp/language/structured_binding。
发布于 2020-12-09 19:00:29
您不能绑定到结构化绑定中的现有变量。
为此,您可以改用std::tie()。
示例:
#include <iostream>
#include <tuple>
int main() {
int a, b, c
std::tie(a, b, c) = std::tuple(1, 2, 3);
std::cout << a << b << c; // prints 123
}或者对于不可复制但可移动的类型:
#include <iostream>
#include <tuple>
struct foo {
foo() = default;
foo(const foo&) = delete; // not copyable for the sake of this demo
foo(foo&&) = default; // but moveable
foo& operator=(const foo&) = delete;
foo& operator=(foo&&) = default;
int a;
};
int main() {
foo w, x, y{3}, z{4};
std::tie(w, x) = std::tuple(std::move(y), std::move(z));
std::cout << w.a << x.a << '\n'; // prints 34
}https://stackoverflow.com/questions/65215237
复制相似问题