플로트 어레이를 멤버 중 하나로 사용하여 C ++ 구조를 선언하는 데 도움이됩니다.

StackOverflow https://stackoverflow.com/questions/627512

문제

누군가 내 구조 선언과 사용에 무엇이 잘못되었는지 여부를 발견 할 수 있는지 궁금했습니다. 현재 나는 구조가 있고 플로트 어레이를 멤버 중 하나로 저장하고 싶습니다.

내 코드 :

struct Player{
float x[12];
float y[12];
float red,green,blue;
float r_leg, l_leg;
int poly[3];
bool up,down;
};

그런 다음 구조물 채우기를 시도했습니다.

float xcords[12] = {1,1,1,1,1,1,1,1,1,1,1,1 };
float ycords[12] = {1,1,1,1,1,1,1,1,1,1,1,1 };
Player player = {xcords,ycords,1,1,1,2,2,true,true};

오류:

1>.\template_with_console.cpp(35) : error C2440: 'initializing' : cannot convert from 'float [12]' to 'float'
1>        There is no context in which this conversion is possible
1>.\template_with_console.cpp(35) : error C2440: 'initializing' : cannot convert from 'float [12]' to 'float'
1>        There is no context in which this conversion is possible
도움이 되었습니까?

해결책

배열은 대부분의 상황에서 배열의 포인터-우선 요소로 부패합니다. xcords 그리고 ycords. 이렇게 구조물을 초기화 할 수 없습니다. 따라서 회원을 명시 적으로 초기화해야합니다.

Player player = {
        {1,1,1,1,1,1,1,1,1,1,1,1 }, // xcords
        {1,1,1,1,1,1,1,1,1,1,1,1 }, // ycords
        1,1,1,                      // red, green, blue
        2,2,                        // right, left
        {0,1,2},                    // poly[3]   -- missing?          
        true,true};                 // up, down

내가 올바르게 이해하면 poly [3]의 초기화기가 누락되었습니다. 적절한 값을 넣으십시오. 그렇지 않으면 기본 초기화가있을 것입니다. 원하는 것이 있습니까?

다른 팁

노력하다

Player player = {{1,1,1,1,1,1,1,1,1,1,1,1 },
                 {1,1,1,1,1,1,1,1,1,1,1,1 },
                 1,1,1,
                 2,2,
                 {},
                 true,true};

초기화가 각 배열의 요소를 구조로 복사 할 것으로 예상하고 있다고 생각합니다. 구조의 배열 요소를 개별적으로 초기화 해보고 for 고리.

다른 배열의 요소를 복사하는 플로트 어레이의 경우 "생성자"가 없습니다.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top