无法访问派生类中的基本受保护成员! (在虚拟功能中)

问题描述 投票:-2回答:1

我有一个抽象的类,例如一个(很少的纯虚函数)。我公开从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++ inheritance compiler-errors protected pure-virtual
1个回答
0
投票

需要所有私有成员引用父项的模板定义。例如首先会改为linkedListType :: first;

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

© www.soinside.com 2019 - 2024. All rights reserved.