Shape
이라는 클래스가 있습니다.이 클래스는 모든 반복 가능 프로그램에서 초기화 할 수 있으며 Array
이라는 클래스에는 단순히 Shape
이 포함되어 있습니다. '모호성을 만들지 않고 std :: initializer_list 생성자를 사용 하시겠습니까?
prog.cxx:35:16: required from here
prog.cxx:14:23: error: request for member ‘begin’ in ‘shape’, which is of non-class type ‘const int’
: Shape(shape.begin(), shape.end()) {}
~~~~~~^~~~~
prog.cxx:14:38: error: request for member ‘end’ in ‘shape’, which is of non-class type ‘const int’
: Shape(shape.begin(), shape.end()) {}
~~~~~~^~~
내가 돈 :
은 컴파일 오류가Shape
의 두 번째 생성자에 나타납니다
class Shape
{
public:
template<typename Iterator>
Shape(Iterator first, Iterator last)
: m_shape(first, last) {}
template <typename Iterable>
Shape(const Iterable& shape)
: Shape(shape.begin(), shape.end()) {}
template<typename T>
Shape(std::initializer_list<T> shape)
: Shape(shape.begin(), shape.end()) {}
private:
std::vector<std::size_t> m_shape;
};
class Array
{
public:
Array(const Shape& shape)
: m_shape(shape) {}
private:
Shape m_shape;
};
int main() {
Shape s{0}; // ok
Array a1({1, 2}); // ok
Array a2({0}); // error
}
: 그러나, 나는 내가 Array
초기화 할 때 내가 설명 할 수 컴파일 오류를 받고 있어요 여기서 무슨 일이 일어나고 있는지 이해해야합니다. initializer_list<T>
생성자 대신 Iterable
생성자가 호출되는 이유는 무엇입니까? {0}
및 Array
생성자가있는 Shape
생성자의 차이점은 무엇입니까?
재현 할 수 없습니다. 내 g ++ 6.3.0 및 내 clang ++ 3.8.1 함께 괜찮아요 ('s5' 줄에 오류 없음) 컴파일하십시오 (제 2 생성자에 대해'NDShape'를 수정하면 Shape에서 분명히 의미합니다.). 어떤 컴파일러를 사용하고 있습니까? – max66
죄송합니다. 나는 코드를 너무 단순화했다.업데이트 된 코드는 이제 오류가 발생합니다. 감사! – AstrOne
이제 오류가 있지만 신고 한 것과 완전히 다릅니다. "cbegin (const int &) [...]"호출에 대한 일치하는 함수가 없음을 확인할 수 있습니까? – max66