C++中将数组值解包到类变量时出现编译错误求助
Let's break down your issues and fix them step by step.
First, let's unpack the errors you're hitting:
Error 1: expected ‘]’ before ‘.’ token
This happens because structured bindings (the auto [ ... ] syntax) only accept simple identifiers on their left side—you can’t directly put member access expressions like SendLowROS.motorCmd[FR_0].Kp inside the brackets. Structured bindings are meant to create new variables that bind to elements/members of the right-hand object, not to assign values directly to existing class members.
Error 2: ‘std::_Vector_base<float, std::allocator<float> >’ is an inaccessible base of ‘std::vector<float>’
std::vector doesn’t support structured bindings. This syntax works with fixed-size arrays, aggregate types (structs/classes with all public non-static members and no user-declared constructors), and tuple-like types (like std::pair or std::tuple). A vector is none of these, and its internal base classes are inaccessible to the compiler when trying to perform binding—hence this error.
Fixes for Your Code
You’ve got a few solid options to get this working:
Option 1: Direct Element Assignment (Simplest & Most Readable)
Just assign each vector element directly to your class members. This is straightforward and easy to follow:
SendLowROS.motorCmd[FR_0].Kp = msg.Kp[0]; SendLowROS.motorCmd[FR_1].Kp = msg.Kp[1]; SendLowROS.motorCmd[FR_2].Kp = msg.Kp[2]; SendLowROS.motorCmd[FL_0].Kp = msg.Kp[3]; SendLowROS.motorCmd[FL_1].Kp = msg.Kp[4]; SendLowROS.motorCmd[FL_2].Kp = msg.Kp[5]; SendLowROS.motorCmd[RR_0].Kp = msg.Kp[6]; SendLowROS.motorCmd[RR_1].Kp = msg.Kp[7]; SendLowROS.motorCmd[RR_2].Kp = msg.Kp[8]; SendLowROS.motorCmd[RL_0].Kp = msg.Kp[9]; SendLowROS.motorCmd[RL_1].Kp = msg.Kp[10]; SendLowROS.motorCmd[RL_2].Kp = msg.Kp[11];
Option 2: Structured Bindings with a Tuple (Cleaner for Pattern Matching)
If you prefer the structured binding pattern, first bind vector elements to temporary variables using a tuple, then assign them to your class members:
// Extract vector elements into a tuple and bind to temp variables auto [kpFR0, kpFR1, kpFR2, kpFL0, kpFL1, kpFL2, kpRR0, kpRR1, kpRR2, kpRL0, kpRL1, kpRL2] = std::make_tuple( msg.Kp[0], msg.Kp[1], msg.Kp[2], msg.Kp[3], msg.Kp[4], msg.Kp[5], msg.Kp[6], msg.Kp[7], msg.Kp[8], msg.Kp[9], msg.Kp[10], msg.Kp[11] ); // Assign to your class members SendLowROS.motorCmd[FR_0].Kp = kpFR0; SendLowROS.motorCmd[FR_1].Kp = kpFR1; SendLowROS.motorCmd[FR_2].Kp = kpFR2; SendLowROS.motorCmd[FL_0].Kp = kpFL0; SendLowROS.motorCmd[FL_1].Kp = kpFL1; SendLowROS.motorCmd[FL_2].Kp = kpFL2; SendLowROS.motorCmd[RR_0].Kp = kpRR0; SendLowROS.motorCmd[RR_1].Kp = kpRR1; SendLowROS.motorCmd[RR_2].Kp = kpRR2; SendLowROS.motorCmd[RL_0].Kp = kpRL0; SendLowROS.motorCmd[RL_1].Kp = kpRL1; SendLowROS.motorCmd[RL_2].Kp = kpRL2;
Option 3: Loop with Target Pointers (Scalable for Future Changes)
If you might add more motors later, this approach keeps your code DRY (Don’t Repeat Yourself):
// Create an array of pointers to the Kp members you want to update std::array<decltype(SendLowROS.motorCmd[0].Kp)*, 12> kpTargets = { &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 }; // Loop through and assign values from msg.Kp for (size_t i = 0; i < kpTargets.size(); ++i) { *kpTargets[i] = msg.Kp[i]; }
All these approaches will resolve your compilation errors and successfully assign the vector values to your class members.
内容的提问来源于stack exchange,提问作者Tahir Mahmood

