Я пробую пример из ISO 2011, сек. 12.9, пункт 7. Ниже приведен код, который я пытаюсь скомпилировать.
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
Это ошибка в gcc? Почему он ищет B1()? Я использую gcc 6.3.0
Редактирование: QUestion в связанном вопросе о том, когда используется один базовый класс, т.е. следующий код
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;
}
Это работает, но когда вводится D2 (int) {chk = 0;}, возникает ошибка.