-4

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.

Tristan
  • 47
  • 1
  • 1
  • 9
  • 3
    Paste the code directly instead of pictures, or it'll be hard to test it out – Ryolu Sep 14 '18 at 01:00
  • 1
    You should provide a MVCE (https://stackoverflow.com/help/mcve) – Phil1970 Sep 14 '18 at 01:37
  • Why your class name in the text above does not match simple code? `class1constructor` is not the same as `addressType`. Such inconsistencies show a lack of effort and don't help to get points for your questions! – Phil1970 Sep 14 '18 at 01:41
  • `personType(const extPersonType &obj)` is not a copy constructor. It is a conversion constructor. – Phil1970 Sep 14 '18 at 01:43
  • I will paste the code later today. – Tristan Sep 14 '18 at 16:56

1 Answers1

1

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.

Phil1970
  • 2,605
  • 2
  • 14
  • 15
  • I find all your comments mostly derogatory rather than helpful. I guess I should have clarified that this is my second year coding, and the assignment specifically says to derive extPersonType from all three classes at once. So I’m going to disregard your rude comment about “laziness” and your idea about using containment. Secondly I didn’t misspell “dataType,” that’s the way it was written in the assignment, just to clarify. As I said before, this is my second year coding and I am still learning the basics of coding, so I’ll ignore your comment about “reading good books.” – Tristan Sep 14 '18 at 16:50
  • As for your other comments, yes I should have pasted the code, but I was hurrying. And I didn’t realize I’d be criticized for naming the pictures slightly different and not verbatim. All I wanted to know is how to correctly make a copy constructor for a class that is derived from three other classes, that is all. I hope you can re-evaluate how your tone came across and help me understand my mistakes. – Tristan Sep 14 '18 at 16:53
  • @Tristan How could I know that you are still a student? In that case, it is a totally different situation that a software developer that would make such compromise only to save a few keystrokes in the short term. – Phil1970 Sep 15 '18 at 04:34
  • I guess I should have prefaced; I've never had to. Is there any help you can give me considering my stance now? – Tristan Sep 15 '18 at 05:31
  • @Tristan The first sample is the easy answer to your compilation error if your teacher explicitly told you to have **derived** type in your **so-called copy constructor**. Otherwise, the next sample is the way to do it, that is to **define real copy constructors**. I think that my answer contains everything you need to do your homework. It is then up to you to take whatever is appropriate given what your teacher has already shown. – Phil1970 Sep 15 '18 at 13:32
  • If you're still willing to help, I used the first sample, and now I'm getting an error that's saying "class 'extPersonType' does not have any field named 'personType'" And now extPersonType class isn't recognizing the personType fields. But it's strange because the other two classes' fields are still working, it's only the personType that is messing up. – Tristan Sep 16 '18 at 18:58
  • Well, if you use the forward declaration (first sample), then the definition of the constructor must be after the actual definition of `personType` class. So it all depends on the declaration and definition order since in C++ you generally need the definition to be before actual use of a class and when there is mutual recursivity, you need at least one forward declaration and splitting declaration and definition of the member (constructor in your case). If you are using multiple files, then you also need to be careful about inclusion order. – Phil1970 Sep 17 '18 at 02:21