Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jsf-2/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 访问超类实例的受保护成员_C++_Inheritance - Fatal编程技术网

C++ 访问超类实例的受保护成员

C++ 访问超类实例的受保护成员,c++,inheritance,C++,Inheritance,(为完整性而编辑) 我有两种结构: struct DoubleLinkedList: public LinkedList { void push(IntEntry entry) override; IntEntry pop(int key) override; protected: DoubleLinkedList *m_previous; }; struct LinkedList { virtual void push(IntEntry entry);

(为完整性而编辑)

我有两种结构:

struct DoubleLinkedList: public LinkedList {
    void push(IntEntry entry) override;
    IntEntry pop(int key) override;
protected:
    DoubleLinkedList *m_previous;
};

struct LinkedList {
    virtual void push(IntEntry entry);
    virtual IntEntry pop(int key);
protected:
    IntEntry m_entry;
    LinkedList* m_next;
};
IntEntry-DoubleLinkedList::pop(int-key)
定义中,我试图访问
m\u-next->m\u-entry
,这给了我一个错误
“m\u-entry”是“LinkedList”的受保护成员。

IntEntry DoubleLinkedList::pop(int key) {
    if (m_next->m_value.key == key) {
       (...)
    } else {
       (...)
    }
}
IntEntry LinkedList::pop(int key)
访问
m\u next->m\u条目时,这不是问题


LinkedList
定义中,有没有一种方法可以访问受保护的成员而不声明
DoubleLinkedList
好友类?与中一样,我根本不想知道
LinkedList
关于
DoubleLinkedList

请确保从修复编译器报告的第一个错误开始,一次处理一个错误。当我试图编译你的代码时,我得到的错误是

“m_值”:不是“LinkedList”的成员

在线

if (m_next->m_value.key == key)
另外,在我定义了一个虚拟对象之后,我得到了这个错误

struct IntEntry
{
    static IntEntry empty() { return IntEntry(); };
};

如果你想得到更多有用的回复,请按照别人的说法发布代码

您已经执行了
static\u cast(…m\u next)
这是处理它的一种方法-即
static\u cast(m\u next)->m\u条目
。这种转换有点危险,因为如果LinkedList以m_next的形式插入LinkedList元素,它将破坏该转换

出现问题的原因是您混合了实现(查找、查看)和接口继承(推送、弹出)-建议不要这样做:


请提供一个。@NathanOliver我正在复制它(使用VS 2010),我应该说我非常惊讶,因为我希望它能起作用。@NathanOliver问题发生在
m_next->m_entry
@Actarus只能访问属于当前对象的当前基本子对象的受保护成员。您无法访问另一个基类的受保护成员。根据@songyuanyao的评论,他说“只有当前基子对象的受保护成员…”(我承认我不知道),在访问成员
m_条目
之前,我将
m_next
转换为
DoubleLinkedList*
,并且它可以工作。因此,如果
m_next
类型为
DoubleLinkedList
的对象的成员总是类型为
DoubleLinkedList*
的,您可以访问m_条目成员列表,如下所示:
static_cast(m_next)->m_条目
。这是一个非常有用的链接,谢谢。我是C++初学者,我对语言特征的完全混淆感到困惑。嘿,对不起,缺少代码。我会更新我的问题。