0

So I have a class address, a class name, and a class person that is derived from name.

class address
{
     public:
     address(char * street, char * zip);

     protected:
     char * street;
     char * zip;
};
class name
{
     public: 
     name( char * initial_name);

     protected:
     char * name;
     address a_address;
};
class person : public name
{
     public:
     person(char * name, char * street, char * zip);
}

the compile error is when I define the person constructor, it looks like this:

person::person(char * initial_name, char * street, char * zip): 
name(initial_name)
{
   a_address.address(street, zip);
}

when I try to access a_address it's telling me invalid use of address::address. Any clues to what I'm doing wrong? Thanks

csStudent
  • 128
  • 2
  • 12
  • You don't have a `address()` member function anywhere. You have a constructor defined. If you intended to call the constructor, you'd do it in the same way you initialized `name` – Justin Jan 18 '18 at 21:44
  • 2
    There's a class `name` which has a member `name`, which is a bad idea. That said, you can access the members but you can not initialize them. At the point the derived class is constructed, the base class has been constructed and initialized already. – Ulrich Eckhardt Jan 18 '18 at 21:44
  • Why are you not using `std::string` – Ed Heal Jan 18 '18 at 21:45
  • Too late to call a constructor. The object has already been instantiated. You can almost `a_address = address(street, zip);`, but you will run afoul of [The Rule of Three.](https://stackoverflow.com/questions/4172722/what-is-the-rule-of-three) – user4581301 Jan 18 '18 at 21:46
  • 1
    Consider that there is an "is a" relationship between a person and a name as you have coded. You should make it a property, so a person "has a" name. – Dragonthoughts Jan 18 '18 at 21:55
  • A name has an address *and* a name, and a person *is* a name? I would expect a person to have a name and an address. – molbdnilo Jan 18 '18 at 22:31

3 Answers3

1

the compile error is when I define the person constructor,

You have a naming confusion here. You have a class called name which has a member called name. It has nothing to do with protected access to the member variable name in class name. You may want to change the variable to a_name or something else that is different than name.

access a_address it's telling me invalid use of address::address

You cannot call a constructor on an object. That's what you are attempting to do with:

a_address.address(street, zip);

You need to create an appropriate constructor in name and pass the arguments from person to name.

Here's an updated version of your posted code that compiles and builds for me.

class address
{
   public:
      address(char * street, char * zip) : street(street), zip(zip) {}

   protected:
      char * street;
      char * zip;
};
class name
{
   public: 
      name(char * initial_name, char * street, char * zip);

   protected:
      char * a_name;
      address a_address;
};

name::name(char * initial_name, char * street, char * zip) :
      a_name(initial_name), a_address(street, zip)
{
}

class person : public name
{
   public:
      person(char * initial_name, char * street, char * zip);
};

person::person(char * initial_name, char * street, char * zip) : 
      name(initial_name, street, zip)
{
}

int main() {}
R Sahu
  • 204,454
  • 14
  • 159
  • 270
0

Your name class has a member variable named name. Your code doesn't compile here because of that.

Also, your person class needs a semicolon after the class declaration.

And you could set a_address like this:

a_address = address(street, zip);
Sid S
  • 6,037
  • 2
  • 18
  • 24
  • `a_address = address(street, zip);` maybe you can do this. You will wind up with two objects pointing to the same `street` and `zip` and possibly a memory leak or Rule of Three violation. – user4581301 Jan 18 '18 at 21:57
  • I disagree. He doesn't reserve any memory at all for those. He should rewrite everything, but my answer was intended to help him past the compiler errors. – Sid S Jan 18 '18 at 22:00
0

You can access the member variable a_address, as it is protected in the base class; but you cannot access the member variables inside the class address, as you did not derive from address. For the class address, you are a stranger, and you have no access to its protected members.

Your structure is not very object oriented, that’s why you run into such troubles. The class address should have a constructor, which you call with its details, and address itself handles the assignemnets to its internal members. If you access members from outside the class, you use them only as simple data structures. That is fine, but it is not object oriented coding, and if you want to work that way , you should change class to struct (as this is exactly the difference between the two), and remove the protected.

Aganju
  • 6,295
  • 1
  • 12
  • 23