So I have this class that has three parent functions, or in other words, it is derived from three other classes.
I'm trying to make a copy constructor, and this is what I have:
// Copy constructor
extPersonType (extPersonType &obj) : addressType(obj), personType(obj), dataType(obj)
{
cout << "Copy constructor active." << endl;
phone = obj.phone;
ident = obj.ident;
}
and here are my copy constructors for the three other classes.
// copy constructor
addressType(extPersonType &obj)
{
street = obj.street;
city = obj.city;
state = obj.state;
zipcode = obj.zipcode;
}
// copy constructor
personType(extPersonType &obj)
{
firstname = obj.firstname;
lastname = obj.lastname;
}
// copy constructor
dataType(extPersonType &obj)
{
day = obj.day;
month = obj.month;
year = obj.year;
}
Keep in mind they each have their own header files and cpp files. Although in this case I used inline function definition.
And yet here is the error I am getting:
[traine@joker Assignment2]$ make
g++ ExtPerson.cpp -c
In file included from ExtPerson.h:5:0,
from ExtPerson.cpp:3:
Data.h:19:26: error: expected ‘)’ before ‘&’ token
dataType(extPersonType &obj)
^
In file included from ExtPerson.h:6:0,
from ExtPerson.cpp:3:
Person.h:18:28: error: expected ‘)’ before ‘&’ token
personType(extPersonType &obj)
^
In file included from ExtPerson.h:7:0,
from ExtPerson.cpp:3:
Address.h:20:29: error: expected ‘)’ before ‘&’ token
addressType(extPersonType &obj)
^
make: *** [ExtPerson.o] Error 1
Anyone know what I'm doing wrong? I'm just confused on how to make copy constructors in a derived class, and how to call the other copy constructors in the other classes. Any help would be appreciated, thanks.
You probably need to add forward declaration since the derived class it not yet declared when you declare the base class.
class extPersonType;
But this is not necessary. Why don't you declare your base constructors according to the normal pattern? That is accepting an argument of the same type as the class. That would works fine and would not depend on the derived class.
personType(const PersonType &obj)
: firstname(obj.firstname)
, lastname(obj.lastname)
{
}
By the way, it is more efficient and show that you know C++ when you use the initializer-list. It will avoid a call to the default constructor followed by a call to the assignment operator.
https://en.cppreference.com/w/cpp/language/initializer_list
However, it is still a bad design to use derivation to put together unrelated objects. An address does not conform to the IS-A relashionship with a person. So it does not make much sense to have extPersonType
derive from addressType
.
You should use containment instead when you extPersonType
class would have an addressType
member.
class extPersonType : public personType
{
addressType address;
dataType birth_date;
};
The only reason to derives from the 3 classes at once would be lazyness. The few seconds you will save initially will make your code harder to maintains as your class will grow. And at some point, you might need to support more than one address or date like the hiring date if which case you will have to make many more changes to the code as the variable become used at many places. You will waste all the time you initially saved and much more.
By the way, this is a bad idea to misspell words in your code. Correct spelling is date and not data given that we can easily see that it is a date from the members and not arbitrary data.
By the way, it could be a good idea to read good books on design and coding as this is very basic stuff that every programmer should master.