2017-05-01 5 views
-1

12.9 파라 7.Follwing 내가상속 생성자 (ISO 2011 초 12.9 파라 7) 나는 ISO 2011 초에서 예를 시도하고

int chk; 
struct B1 { B1(int){chk=9;} }; 
struct B2 { B2(int){chk=10;} }; 
struct D2 : B1, B2 { 
     using B1::B1; 
     using B2::B2; 
     D2(int){chk=0;}; 
}; 
int main(){ 
    B1 b(9); 
    D2 d(0); 
    return 0; 
} 

$ g ++ -std = C++를 컴파일 할 트링하고있는 코드는 ? 연결 문제 질문 아부입니다 :? 11 sample.cpp 오류 메시지

<source>: In constructor 'D2::D2(int)': <source>:7:10: error: no matching function for call to 'B1::B1()' D2(int){chk=0;}; 
    ^<source>:2:15: note: candidate: B1::B1(int) struct B1 { B1(int){chk=9;} }; 
      ^~ <source>:2:15: note: candidate expects 1 argument, 0 provided <source>:2:8: note: candidate: constexpr B1::B1(const B1&) struct B1 { B1(int){chk=9;} }; 
    ^~ <source>:2:8: note: candidate expects 1 argument, 0 provided <source>:2:8: note: candidate: constexpr B1::B1(B1&&) <source>:2:8: note: candidate expects 1 argument, 0 provided <source>:7:10: error: no matching function for call to 'B2::B2()' D2(int){chk=0;}; 
    ^<source>:3:15: note: candidate: B2::B2(int) struct B2 { B2(int){chk=10;} }; 
      ^~ <source>:3:15: note: candidate expects 1 argument, 0 provided <source>:3:8: note: candidate: constexpr B2::B2(const B2&) struct B2 { B2(int){chk=10;} }; 
    ^~ <source>:3:8: note: candidate expects 1 argument, 0 provided <source>:3:8: note: candidate: constexpr B2::B2(B2&&) <source>:3:8: note: candidate expects 1 argument, 0 provided Compiler exited with result code 1 

이 왜 6.3.0

편집 B1() 내가 사용하고 GCC를 찾고 있습니다 GCC의 버그 t 한 기본 클래스 used.i.e 때 D2 (INT) {CHK = 0;} 때 작동되지만 코드

int chk; 
struct B1 { B1(int){chk=9;} }; 
//struct B2 { B2(int){chk=10;} }; 
struct D2 : B1 { 
    using B1::B1; 
    //using B2::B2; 
    //D2(int){chk=0;}; 
}; 
int main(){ 
    B1 b(9); 
    D2 d(0); 
    return 0; 
} 

다음 에러가 발생할 때 그게를 도입한다.

답변

0

D에는 기본 클래스 B1B2의 생성자를 명시 적으로 호출하지 않는 유일한 생성자 D2(int){chk=0;}을 제공합니다. 따라서 컴파일러는 암시 적으로 호출되는 B1B2의 기본 생성자를 찾습니다. 하지만 당신의 기본 구조 B1B2는 따라서 ...

를 기본 생성자를 제공하거나 B1B2에 기본 생성자를 정의하거나 D의 생성자의 initialiser 목록에 명시 적으로 다른 생성자를 호출하지 않습니다

D2(int x):B1(x),B2(x) {chk=0;}; 
+0

다음과 같이 코드를 변경했습니다. int chk; struct B1 {B1 (int) {chk = 9;}}; struct B2 {B2 (double, float) {chk = 10;}}; 구조체 D2 : B1, B2 { B1 :: B1; B2 :: B2를 사용합니다. // D2() {} // D2 (int) {chk = 0;}; }; int main() { B1 b (9); D2 d (10); return 0; }'여전히 오류가 발생합니다.이 경우 기본 클래스 'B1'에서 'D2'에 의해 상속 된 생성자가 암시 적으로 삭제됩니다. 왜 삭제됩니까? ISO의 기본 생성자에 대한이 특정 규칙을 찾으려고합니다. doc.ISO 문서에 대한 참조를 가리킬 수 있습니까? @Stephan Lechner – user3547730