[英]Compile error when trying to access protected data member of base class?
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. 当我尝试访问a_address时,它告诉我对address :: address的无效使用。 Any clues to what I'm doing wrong?
关于我在做什么错的任何线索? Thanks
谢谢
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
. 您有一个名为
name
的类,该类具有一个名为name
的成员。 It has nothing to do with protected
access to the member variable name
in class name
. 它与对类
name
的成员变量name
protected
访问无关。 You may want to change the variable to a_name
or something else that is different than name
. 您可能需要将变量更改为
a_name
或不同于name
其他name
。
access a_address it's telling me invalid use of address::address
访问a_address,它告诉我对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
. 您需要在
name
创建一个适当的构造函数,并将参数从person
传递到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() {}
Your name
class has a member variable named name
. 您的
name
类具有一个名为name
的成员变量。 Your code doesn't compile here because of that. 因此,您的代码无法在此处编译。
Also, your person
class needs a semicolon after the class declaration. 同样,您的
person
课程在课程宣告后需要使用分号。
And you could set a_address
like this: 您可以这样设置
a_address
:
a_address = address(street, zip);
You can access the member variable a_address
, as it is protected
in the base class; 您可以访问成员变量
a_address
,因为它在基类中protected
; but you cannot access the member variables inside the class address
, as you did not derive from address
. 但是您不能访问类
address
内的成员变量,因为您不是从address
派生的。 For the class address
, you are a stranger, and you have no access to its protected
members. 对于类
address
,您是一个陌生人,并且无法访问其protected
成员。
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. 类
address
应该有一个构造函数,您可以使用其构造函数对其进行详细说明,并且该address
本身可以处理为其内部成员分配的代币。 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. 很好,但是不是面向对象的编码,如果您要那样工作,则应将
class
更改为struct
(因为这恰恰是两者之间的区别),并删除受保护的代码。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.