【问题标题】:Not able to access base protected members in derived class ! ( in virtual functions )无法访问派生类中的基本受保护成员! (在虚函数中)
【发布时间】:2019-09-01 04:35:05
【问题描述】:

我有一个抽象类,例如A(少数纯虚函数)。我是从 A 类公开继承 B 类的。 在提供虚函数的定义时,我试图访问 A 类的私有成员(例如 Ptr )。但是,编译器无法找到类 A 的所有受保护成员的声明。 这里出了什么问题?

        //
        // LinkedListType.h
        //

        #ifndef LINKEDLISTTYPE_LINKEDLISTTYPE_H
        #define LINKEDLISTTYPE_LINKEDLISTTYPE_H

        #include <iostream>
        using namespace std;

        template <class Type>
        struct nodeType
        {
            int val;
            nodeType<Type> *link;
        };

        template <class Type>
        class LinkedListType{
        public:

            virtual void insertFirst(Type& node)=0;

            LinkedListType();
            ~LinkedListType();
            void destroyList();
        protected:
            int count;
            nodeType<Type> *first;
            nodeType<Type> *last;
        };
        #endif //LINKEDLISTTYPE_LINKEDLISTTYPE_H


        //
        // LinkedListType.cpp
        //
        #include <iostream>
        #include "LinkedListType.h"

        using namespace std;

        template <class Type>
        void LinkedListType<Type>::destroyList() {
            if (firstElem == NULL)cout<<"LinkedList is empty"<<endl;

            nodeType<Type> *tmp;
            while(tmp != NULL){
                tmp=first;
                first=first->link;
                delete tmp;
            }
            last=NULL;
            count =0;
        }



        template <class Type>
        LinkedListType<Type>::LinkedListType() {
            first=NULL;
            last=NULL;
            count=0;
        }


        template <class Type>
        LinkedListType<Type>::~LinkedListType() {
            destroyList();
        }




//
        // unorderedLinkedList.cpp
        //

        #ifndef UNORDEREDLINKEDLIST_UNORDEREDLINKEDLIST_H
        #define UNORDEREDLINKEDLIST_UNORDEREDLINKEDLIST_H

        #include <iostream>
        #include "LinkedListType.h"

        using namespace std;

        template <class Type>
        class unorderedLinkedList: public LinkedListType<Type>{
        public:
            void insertFirst(Type& node);
        };
        #endif //UNORDEREDLINKEDLIST_UNORDEREDLINKEDLIST_H



        //
        // unorderedLinkedList.h
        //
        #include <iostream>
        #include "unorderedLinkedList.h"

        using  namespace std;

        template <class Type>
        void unorderedLinkedList<Type>::insertFirst(Type& node) {
            nodeType<Type> *newNode= new nodeType<Type>;
            if(newNode == NULL)
                assert("Unable to allocate node to insert");
            newNode->val=node;
            newNode->link=first;  /* ---> ERROR error: use of undeclared 
                               identifier 'first'; did you mean 
                               'std::_PairT::first'? */
            first=newNode; /* ---> ERROR error: use of undeclared 
                                identifier 'first'; did you mean 
                               'std::_PairT::first'? */
            count++;
        }



        //
        // CMakeList.txt
        //
        cmake_minimum_required(VERSION 3.13)
        project(UnorderedLinkedList)

        set(CMAKE_CXX_STANDARD 14)

        add_executable(UnorderedLinkedList main.cpp LinkedListType.h LinkedListType.cpp unorderedLinkedList.h unorderedLinkedList.cpp)


        //Main program
        #include <iostream>
        #include "unorderedLinkedList.h"
        #include "unorderedLinkedList.cpp"
        using namespace std;

        int main() {
            std::cout << "Hello, World!" << std::endl;
            unorderedLinkedList<int> u1;
            u1.insertFirst(10);
            return 0;
        }

【问题讨论】:

  • 此代码示例甚至无法编译(c++ 是区分大小写的语言)。请在此处提供minimal reproducible example 以重现您的问题。
  • 查看错误消息的其余部分会很有帮助。我说你在重要的部分切断了它。
  • 在纠正示例的基本语法错误(例如,Public -> public)时,它可以毫无问题地编译。请提供一个复制问题的最小示例。
  • 大家好,这不是实际代码,而是整体代码流程。我将尝试发布最少的完整代码!
  • @drescherjm 请立即查找更新的代码!

标签: c++ inheritance compiler-errors protected pure-virtual


【解决方案1】:

所有私有成员都需要与父级的模板定义一起引用。 例如 first 将更改为 linkedListType::first;

这是因为使用模板库的一些编译器限制。

【讨论】:

    猜你喜欢
    • 2012-08-29
    • 2019-01-20
    • 2016-04-07
    • 2016-02-29
    • 2012-05-02
    • 2013-10-21
    • 2014-08-27
    • 2015-12-30
    • 2018-11-27
    相关资源
    最近更新 更多