The problem here is about the default constructor
of the Node
class. The default constructor, is automatically generated in class
, if there is no other user-defined constructors. So, the default constructor tries to generate the default values to all of the class's members, and run into problems when it tries to call the default constructor of the object Customer
which is no exists (because the constructor Customer(string,char,int);
exists, and there is no user-defined default constructor for the Customer
class). There are several solutions you can use (Sorted from most recommended to least recommended, in this particular case. in other cases this rate might have some changes):
Solution 1 (Recommended)
Define a constructor in the
Node
class, that use the non-default constructor of the
Customer
class member
parent
:
class Node {
public:
Node(string name, char c, int age) : parent(name, c, age) {}
Customer parent;
Node* left;
Node* right;
};
Solution 2
Define default constructor for Customer
class:
class Customer {
public:
/*...*/
Customer();
/*...*/
}
Why doesn't it the most recommended way in this case? It solves the compiler's error with less code than the most recommended way, so why not choosing this way? This way have logical issue- What does an empty customer mean? How do you know when a Customer
object is empty? You can see my answer here: C++ vector of struct allocated on stack to get an idea of identify an object that have been created without any user-defined data. Those validations are a little bit more complicated than forcing the user to set some data ahead.
However - this way might be the best way when there are no logical issues like the one I mentioned before, or whenever there is no need to deal with validations, or in cases where the validations are worth the choosing in this solution.
Solution 3 (Highly not recommended)
Define
parent
in
Node
class as pointer (so the default value will be
nullptr
).
Why not choosing this option? This option might be the easiest and fastest way to make the compiler error disappear- However it will force you to handle pointers of pointers and memory management all the way in your program, and will make your life much harder than the other solutions (after the compiler error gone - segfault
(What is a segmentation fault?) are much more annoying than compiler errors).