문제

프로그램에서 몇 가지 다른 구조를 만들었습니다. 이제 중첩 된 구조가있는 구조가 있지만 올바르게 초기화하는 방법을 해결할 수는 없습니다. 구조는 아래에 나열되어 있습니다.

/***POINT STRUCTURE***/
struct Point{
    float x;                    //x coord of point
    float y;                    //y coord of point
};

/***Bounding Box STRUCTURE***/
struct BoundingBox{
    Point ymax, ymin, xmax, xmin;
};

/***PLAYER STRUCTURE***/
struct Player{
    vector<float> x;            //players xcoords
    vector<float> y;            //players ycoords
    BoundingBox box;
    float red,green,blue;       //red, green, blue colour values
    float r_leg, l_leg;         //velocity of players right and left legs
    int poly[3];                //number of points per polygon (3 polygons)
    bool up,down;               
};

그런 다음 새로 생성 된 플레이어 Struct를 플레이어라고 생각합니다.

//Creates player, usings vectors copy and iterator constructors
Player player = { 
vector<float>(xcords,xcords + (sizeof(xcords) / sizeof(float)) ), //xcords of player
vector<float>(ycords,ycords + (sizeof(ycords) / sizeof(float)) ), //ycoords of playe
box.ymax = 5;               //create bounding box
box.ymin = 1;
box.xmax = 5;
box.xmin = 1;
1,1,1,                      //red, green, blue
0.0f,0.0f,                  //r_leg,l_leg
{4,4,4},                    //number points per polygon
true,false};                //up, down

이로 인해 상자와 관련하여 여러 가지 오류가 발생합니다. 상자에 명시한 것은 명확한 식별자가없고 구조물 또는 구문이 누락되기 전에 '.'.

그런 다음 Player Struct를 만들고 다음과 같이 멤버를 초기화하려고했습니다.

Player bob;
bob.r_leg = 1;

그러나 컴파일러가 Bob에 식별자가 없거나 일부 구문이 누락되었다고 생각하기 때문에 더 많은 오류가 발생합니다.

나는 문제를 해결했지만 (부모) 구조 내에서 중첩 된 구조의 여러 구성원을 비활성화하는 방법을 보여주는 기사를 찾지 못했습니다. 이 주제에 대한 모든 도움은 크게 감사 할 것입니다 :-) !!!

도움이 되었습니까?

해결책

정상적으로 초기화합니다 { ... }:

Player player = { 
  vector<float>(xcords,xcords + (sizeof(xcords) / sizeof(float)) ),
  vector<float>(ycords,ycords + (sizeof(ycords) / sizeof(float)) ),
  5, 1, 5, 1, 5, 1, 5, 1, 
  1.0f,1.0f,1.0f,                         //red, green, blue
  0.0f,0.0f,                              //r_leg,l_leg
  {4,4,4},                                //number points per polygon
  true,false
};   

이제 "Brace Elision"을 사용하고 있습니다. 일부 컴파일러는 독자들을 혼동 할 수 있기 때문에 완전히 표준이지만 경고합니다. 버팀대를 추가하므로 초기화되는 내용이 분명해집니다.

Player player = { 
  vector<float>(xcords,xcords + (sizeof(xcords) / sizeof(float)) ),
  vector<float>(ycords,ycords + (sizeof(ycords) / sizeof(float)) ),
  { { 5, 1 }, { 5, 1 }, { 5, 1 }, { 5, 1 } }, 
  1.0f, 1.0f, 1.0f,               //red, green, blue
  0.0f, 0.0f,                     //r_leg,l_leg
  { 4, 4, 4 },                    //number points per polygon
  true, false
};

초기화하려는 경우 x 포인트의 구성원은 다른 이니셜 라이저를 생략하여 그렇게 할 수 있습니다. 응집체 (배열, 스트러크)의 나머지 요소는 "오른쪽"값으로 초기화됩니다. NULL 포인터의 경우 a false BOOL의 경우, ints 등의 ZERO 및 사용자 정의 유형의 생성자를 대략 사용합니다. 포인트를 초기화하는 행은 다음과 같습니다

{ { 5 }, { 5 }, { 5 }, { 5 } }, 

이제 Brace Elision을 사용하는 위험을 볼 수 있습니다. 구조물에 일부 멤버를 추가하면 모든 초기화기가 초기화 해야하는 실제 요소를 "이동"하며 실수로 다른 요소를 때릴 수 있습니다. 따라서 항상 적절한 곳에 버팀대를 사용하는 것이 좋습니다.

그래도 생성자 사용을 고려하십시오. 방금 Brace Enplosed Initializers를 사용하여 어떻게 수행 할 것인지를 보여주는 코드를 완료했습니다.

다른 팁

SO와 같은 구조에 기본값을 추가 할 수 있습니다.

struct Point{
   Point() : x(0), y(0)
     { };

   float x;
   float y;
};

또는

struct Point{
   Point() 
     { 
       x = 0;
       y = 0;
     };

   float x;
   float y;
};

시공 중에 해당 값을 추가하려면 다음과 같은 생성자에 매개 변수를 추가하십시오.

struct Point{
   Point(float x, float y) 
     { 
       this->x = x;
       this->y = y;
     };

   float x;
   float y;
};

그리고 이것들을 인스턴스화합니다.

Point Pos(10, 10);
Point *Pos = new Point(10, 10);

이것은 다른 데이터 구조에도 적용됩니다.

경계 상자에는 지점이 아닌 플로트가 포함되어 있어야합니까?

그것은 이것이 효과가 있다고 말했습니다.

Player player = { 
vector<float>(xcords,xcords + (sizeof(xcords) / sizeof(float)) ), //xcords of player
vector<float>(ycords,ycords + (sizeof(ycords) / sizeof(float)) ), //ycoords of playe
{{1.0,5.0},{1.0,5.0},{1.0,5.0},{1.0,5.0}},
1,1,1,                                          //red, green, blue
0.0f,0.0f,                              //r_leg,l_leg
{4,4,4},                                //number points per polygon
true,false};  

(테스트되지 않은) ...

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