+ 5

Why this code shows error?

Composition in C++ https://sololearn.com/compiler-playground/cCxEG5urSExU/?ref=app

16th Feb 2024, 1:50 PM
Yogeshwaran P
Yogeshwaran P - avatar
11 Respuestas
+ 8
because you defined a Birthday constructor that accepts arguments, the default constructor that does not accept arguments is overriden. You have to redeclare it to make it clear that it is still valid to use.
16th Feb 2024, 2:20 PM
Bob_Li
Bob_Li - avatar
+ 5
Look at the argument in the Person class, you've defined a Birthday that doesn't have constructor. The only thing you should do is to define another constructor without any arguments: // In class Birthday: public: Birthday() {} ..... //and continue coding...
16th Feb 2024, 3:53 PM
🇮🇱 Radin Masiha 🇮🇱
🇮🇱 Radin Masiha 🇮🇱 - avatar
+ 3
Yogeshwaran P add a public default constructor to your Birthday class. class Birthday { public: Birthday() = default; ... ...
16th Feb 2024, 1:57 PM
Bob_Li
Bob_Li - avatar
+ 3
Yogeshwaran P If we define a constructor whether it is parameterized or copy constructor than compiler will not create the default constructor implicitly. Compiler giving you error because bd property in person class need arguments but we can't initialize the properties outside the method. So, constructor member initialiser can be used or define the default constructor in Birthday class explicitly.
16th Feb 2024, 5:54 PM
Rishi
Rishi - avatar
+ 3
Bob_Li and 🇮🇱 Radin Masiha 🇮🇱 and Rishi, this is one of many hidden subtleties in C++ that prevents me from becoming an expert. What do you think of an alternative solution by making bd a pointer to Birthday? It avoids adding another constructor. class Person { public: Person(string n, Birthday &b) { name = n; bd = &b; } void printInfo() { cout << name << endl; bd->printDate(); } private: string name; Birthday *bd; };
17th Feb 2024, 4:01 PM
Brian
Brian - avatar
+ 3
Brian Yeah I agree that's a nice way. It can be either done the way you did, or it can be done by sending the address to the constructor and changing it to pointer right there. I think this solution is better cuz it avoids adding another constructor, but you cannot always avoid this, sometimes you should have more than one constructor, but here you can do it and I think it's more logical.
17th Feb 2024, 4:42 PM
🇮🇱 Radin Masiha 🇮🇱
🇮🇱 Radin Masiha 🇮🇱 - avatar
+ 2
Bob_Li i think c++ is very Hard to understand,still I don't got it
16th Feb 2024, 2:44 PM
Yogeshwaran P
Yogeshwaran P - avatar
+ 2
But in online,i find that default constructor created by compiler implicitly when it is not created by us(explicitly)
16th Feb 2024, 4:28 PM
Yogeshwaran P
Yogeshwaran P - avatar
+ 2
Brian It's a valid and probably more efficient way. The original question was why using default constructors with no arguments are raising errors. You can still test it in the question's code if you instantiated Person without any arguments. int main(){ Person p2; } This will raise an error because class Person also does not have access to the default constructor because of the constructor overload.
17th Feb 2024, 4:48 PM
Bob_Li
Bob_Li - avatar
+ 1
yes, so many obscure rules... also, you don't have a way to change the birthday in your code...
16th Feb 2024, 2:50 PM
Bob_Li
Bob_Li - avatar
0
Bob_Li Why it work with default,but not working without it, What is use of default ? i also find that it works fine if I initialise the birthday member using constructor member initialiser
16th Feb 2024, 2:02 PM
Yogeshwaran P
Yogeshwaran P - avatar