C++ 类构造函数中未定义的长度数组

C++ 类构造函数中未定义的长度数组,c++,C++,我一直在尝试在类构造函数中使用数组。这是我的密码: struct Motor_Group{ int Motors[3]; int Encoder; }; int main() { Motor_Group Left_Drive {{2,3},3}; Motor_Group Right_Drive {{2,3},3}; cout<< sizeof(Left_Drive.Motors)/sizeof(int); return 0; } s

我一直在尝试在类构造函数中使用数组。这是我的密码:

struct Motor_Group{
    int Motors[3];
    int Encoder;
}; 
int main()
{
    Motor_Group Left_Drive {{2,3},3};
    Motor_Group Right_Drive {{2,3},3};
    cout<< sizeof(Left_Drive.Motors)/sizeof(int);
    return 0;
}
struct Motor\u组{
int电机[3];
int编码器;
}; 
int main()
{
马达组左驱动{{2,3},3};
电机组右驱动{{2,3},3};

cout如果不需要相同类型的电机组, 然后您可以模板化数组大小

使用

#包括
#包括
模板
结构电机组{
阵列电机;
int编码器;
}; 
int main()
{
马达组左驱动{{2,3},3};
电机组右驱动{{2,3,4},3};

在声明本地数组时,构造函数没有什么特别之处。在构造函数中声明和使用数组的方式与在任何其他函数或类方法中声明和使用数组的方式完全相同。无论您当前在其他函数或类方法中使用数组时做了什么,您只需准确地执行即可这里也是一样。不清楚你到底在问什么。试试std::vector?数组大小必须在编译时知道
#include <array>
#include <iostream>

template<size_t motor_count>
struct Motor_Group{
    std::array<int,motor_count> Motors;
    int Encoder;
}; 
int main()
{
    Motor_Group<2> Left_Drive {{2,3},3};
    Motor_Group<3> Right_Drive {{2,3,4},3};
    std::cout<< Left_Drive.size();
    // Left_Drive = Right_Drive; // Error at compile time, since Motor_Group<2> != Motor_Group<3>
    return 0;
}