所以我有一个问题,我一直在争取几个小时。 SO上有各种各样的问题抱怨同样的问题,但没有解决方案适合我。
我有两个结构
// \brief The state of a single joint position. Default value of the speed is the maximum it wil allow.
struct JointPosition
{
/// \brief The degree to set the joint to.
double degree = 0;
/// \brief The max degrees per second it will allow during the move.
double maxDegreesPerSecond = 0;
};
/// \brief Struct containing all joint positions as degrees.
struct JointPositions
{
JointPosition base;
JointPosition shoulder;
JointPosition elbow;
JointPosition wrist;
JointPosition gripper;
JointPosition wristRotate;
};
我想像这样支持初始化:
static const JointPositions pos = {
{0, 0},
{0, 0},
{0, 0},
{0, 0},
{0, 0},
{0, 0}
};
return pos;
但是当我这样做时,我的编译器会抱怨以下错误:
RobotArm.cpp:59:2: error: could not convert ‘{0, 0}’ from ‘<brace-enclosed initializer list>’ to ‘JointPosition’
Afaik大括号初始值设定项应该与结构一起使用,只要它们没有构造函数。
我正在使用c ++ 11和gcc 7.3。
感谢任何帮助。
以下是展示此问题的在线链接:
答案 0 :(得分:1)
答案 1 :(得分:0)
我认为这是因为你已经定义了&#34; JointPosition&#34;值为&#34; 0&#34;在结构中。如果你删除它们就会完成。
答案 2 :(得分:0)
您已经初始化了两个数据成员,其定义为
struct JointPosition
{
/// \brief The degree to set the joint to.
double degree = 0;
/// \brief The max degrees per second it will allow during the move.
double maxDegreesPerSecond = 0;
};
所有成员都已初始化。
如果你想在声明后初始化它们,那么只需从struct JointPosition的上面定义中删除零,你的程序运行正常。