2017-10-07 6 views
1

그래서 몇 시간 동안 계속해서 논쟁을 벌여 왔습니다. 이 같은 문제에 대해 불평하지만 솔루션은 나를 위해 작동하지 SO에 대한 다양한 질문 ..중첩 구조체 초기화

2 구조체

// \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; 
}; 

내가 한 수 있습니다 그리고 나는이처럼 initialze 지탱하려면 :

내가 그렇게 할 때
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 중괄호 이니셜 라이저는 생성자가없는 한 구조체와 함께 작동해야합니다.

gcc 7.3에서 C++ 11을 사용하고 있습니다.

도움을 주시면 감사하겠습니다.

https://onlinegdb.com/HkKzwoLhb

+0

@VittorioRomeo 아니요 6 개의 JointPosition을 가진 단일 JointPositions를 만들고 싶습니다. –

+0

@Someprogrammerdude 문제를 시연하는 onlinegdb에 대한 링크를 추가했습니다. –

+3

제공하는 링크는 "C++"라고만 말합니다. "C++ 14". 보통 C++ 03이라는 것을 의미합니다. 그러나'JointPosition'에서 인라인 초기화를 제거하면 오류없이 컴파일해야하며 (C++이 개발 된 이후와 C에서 그 전에 구현 된 적이있다) 컴파일해야합니다. –

답변

1

문제는 사용하는 C++ 버전 :

다음은 문제를 보여 온라인 링크입니다.

Live Demo에서 GCC 7.2.0 및 C++ 11로 문제를 재현 할 수 있습니다.

C++ 14로 전환하면 오류가 즉시 해결됩니다.

+1

예 C++ 14가 실제로이 문제를 수정합니다. –

0

"JointPosition"값을 구조에서 "0"으로 정의했기 때문에 생각합니다. 삭제하면 삭제가 완료됩니다.

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; 
}; 

모든 구성원이 이미 초기화의 정의에서 0으로 두 데이터 멤버를 초기화했다.

선언 후에 초기화하려면 구조체 JointPosition의 위 정의에서 0을 제거하면 프로그램이 올바르게 실행됩니다.