C++:为什么我的 DerivedClass 构造函数无法访问 BaseClass 的受保护字段?

发布于 2024-09-11 07:30:52 字数 1176 浏览 13 评论 0原文

我有一个构造函数尝试初始化基类中的字段。编译器抱怨。该字段受保护,因此派生类应该具有访问权限。

//The base class: 

class BaseClass

{

public:

    BaseClass(std::string);
    BaseClass(const BaseClass& orig);
    virtual ~BaseClass();
    const std::string GetData() const;
    void SetData(const std::string& data);
protected:

    BaseClass();
    std::string m_data;

};

BaseClass::BaseClass(const std::string data) : m_data(data) { }

BaseClass::BaseClass() { } 

BaseClass::BaseClass(const BaseClass& orig) { }

BaseClass::~BaseClass() { }

void BaseClass::SetData(const std::string& data)
{
    m_data = data;
}

const std::string BaseClass::GetData() const
{
    return m_data;
}


//The derived class: 


class DerivedClass : public BaseClass
{

public:

    DerivedClass(std::string data);
    DerivedClass(const DerivedClass& orig);
    virtual ~DerivedClass();
private:

};

DerivedClass::DerivedClass(std::string data) : m_data(data) { } //ERROR HERE

DerivedClass::DerivedClass(const DerivedClass& orig) { }

DerivedClass::~DerivedClass() { }

//编译器错误

DerivedClass.cpp:3: 错误: 类 'DerivedClass' 没有任何名为 'm_data' 的字段

非常感谢任何帮助。先感谢您。

I have a constructor attempting to initialize a field in a base class. The compiler complains. The field is protected, so derived classes should have access.

//The base class: 

class BaseClass

{

public:

    BaseClass(std::string);
    BaseClass(const BaseClass& orig);
    virtual ~BaseClass();
    const std::string GetData() const;
    void SetData(const std::string& data);
protected:

    BaseClass();
    std::string m_data;

};

BaseClass::BaseClass(const std::string data) : m_data(data) { }

BaseClass::BaseClass() { } 

BaseClass::BaseClass(const BaseClass& orig) { }

BaseClass::~BaseClass() { }

void BaseClass::SetData(const std::string& data)
{
    m_data = data;
}

const std::string BaseClass::GetData() const
{
    return m_data;
}


//The derived class: 


class DerivedClass : public BaseClass
{

public:

    DerivedClass(std::string data);
    DerivedClass(const DerivedClass& orig);
    virtual ~DerivedClass();
private:

};

DerivedClass::DerivedClass(std::string data) : m_data(data) { } //ERROR HERE

DerivedClass::DerivedClass(const DerivedClass& orig) { }

DerivedClass::~DerivedClass() { }

//The compiler error

DerivedClass.cpp:3: error: class ‘DerivedClass’ does not have any field named ‘m_data’

Any help is greatly appreciated. Thank you in advance.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

jJeQQOZ5 2024-09-18 07:30:52

您不能在派生类构造函数中初始化 m_data,而是将其作为参数传递给基类构造函数。

即:DerivedClass::DerivedClass(std::string data) : BaseClass(data) { }

You cannot initialize m_data in the derived class constructor but instead pass it as an argument to the base class constructor.

That is: DerivedClass::DerivedClass(std::string data) : BaseClass(data) { }

两相知 2024-09-18 07:30:52

在初始值设定项列表中,您只需为同一类的属性设置值。要访问它,您必须在构造函数主体中赋予该值:

DerivedClass::DerivedClass(std::string data) {m_data = data; }

,如果复制对象的成本很高,则可以将 m_data 作为参数传递给父类构造函数:

DerivedClass::DerivedClass(std::string data) : BaseClass(data) {}

提示:将数据作为引用传递以防止复制构造函数。

请在此处查看更多信息:C++ 构造函数的初始化顺序

In the initializer list you can just set values for attributes of the same class. To access it, you must attribute the value in the body of the constructor:

DerivedClass::DerivedClass(std::string data) {m_data = data; }

Or, if it is expensive to copy the object, you pass the m_data as an argument to the parent class constructor :

DerivedClass::DerivedClass(std::string data) : BaseClass(data) {}

Tip: Pass your data as a reference to prevent the copy constructor.

See more info here: order of initialization of C++ constructors.

空城旧梦 2024-09-18 07:30:52

你不是在“访问”m_data——你是在初始化它。但是,它已经在基类的构造函数中进行了初始化。如果您想更改它的值,请在您的 ctor 主体中分配给它:

DerivedClass::DerivedClass(std::string data) 
{
   m_data = data;
}

You are not "accessing" m_data -- you are initializing it. However, it's already been initialized in the Base Class's ctor. If you want to change it's value, assign to it in the body of your ctor:

DerivedClass::DerivedClass(std::string data) 
{
   m_data = data;
}
不弃不离 2024-09-18 07:30:52

您需要按如下方式调用基类构造函数:

DerivedClass::DerivedClass(std::string data) : BaseClass(data) {
}

每个类都应负责初始化其成员。

You need to call the base class constructor as follows:

DerivedClass::DerivedClass(std::string data) : BaseClass(data) {
}

Each class should be in charge of initializing it's members.

不羁少年 2024-09-18 07:30:52

初始化列表只能用于初始化相关类型所拥有的字段。在初始化器列表中初始化基类字段是不合法的,这就是您收到此错误的原因。该字段可以在 DerivedClass 中访问

Initializer lists can only be used to initialize fields which are owned by the type in question. It is not legal to initialize base class fields in an initializer lists which is why you receive this error. The field is otherwise accessible within DerivedClass

々眼睛长脚气 2024-09-18 07:30:52

您的派生类构造函数确实具有访问权限,但您无法在初始化列表中分配给它。将构造函数更改为:

DerivedClass::DerivedClass(const std::string& data)
 : BaseClass(data)
{
}

或者,如果没有合适的基类构造函数可用并且您无法添加一个,则可以将构造函数更改为:

DerivedClass::DerivedClass(const std::string& data)
{
    m_data = data;
}

Your derived class constructor does have access, but you cannot assign to it in the initialisation list. Change the constructor to:

DerivedClass::DerivedClass(const std::string& data)
 : BaseClass(data)
{
}

Alternatively, if no suitable base class constructor is available and you cant add one you can change the constructor to:

DerivedClass::DerivedClass(const std::string& data)
{
    m_data = data;
}

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文