티스토리 뷰

C++

[C++] C++ std::array<> too many initializers

developer0hye 2023. 1. 30. 19:54

https://stackoverflow.com/questions/62252076/c-array-too-many-initializers

 

C++: array<> too many initializers

The following code is returning the compilation error below. I'm stuck understanding how there are too many initializers. This code works using vector<X>. Does anyone know why the error is...

stackoverflow.com

https://stackoverflow.com/questions/26629609/too-many-initializers-for-array-error

 

Too many initializers for Array error

I have implemented following structures: struct Point { int x,y; }; struct Array { Point elem[3]; }; Could you explain why I'm getting an error: error: too many initializers for 'Arr...

stackoverflow.com

 

std::pair 를 자료형으로 갖는 std::array 객체를 생성하려는데 초기화 과정에서 오류가 났다.

 

아래는 코드 예시인데 첫줄처럼 쓰려니 안되고 둘째줄 처럼 써야 됐다.

std::array<std::pair<int, int>, 4> dirs{ {1, 0}, {0, 1}, {-1, 0}, {0, -1} }; // error
std::array<std::pair<int, int>, 4> dirs{ { {1, 0}, {0, 1}, {-1, 0}, {0, -1} } }; // ok

 

괄호별로 아래와 같은 의미를 갖는다.

std::array<std::pair<int, int>, 4> dirs{ { {1, 0}, {0, 1}, {-1, 0}, {0, -1} } };
                                       | | |
                                       | | 초기화시 저장될 std::pair의 괄호를 의미
                                       | 초기화시 저장될 std::pair 배열의 괄호를 의미
                                       std::array class의 초기화를 알리는 괄호를 의미
댓글
공지사항
최근에 올라온 글
최근에 달린 댓글
Total
Today
Yesterday
링크
«   2024/05   »
1 2 3 4
5 6 7 8 9 10 11
12 13 14 15 16 17 18
19 20 21 22 23 24 25
26 27 28 29 30 31
글 보관함