简体   繁体   English

无法访问派生类中的基类保护成员! (在虚函数中)

[英]Not able to access base protected members in derived class ! ( in virtual functions )

I have a abstract class eg A ( few pure virtual functions ).我有一个抽象类,例如 A (几个纯虚函数)。 I am public inheriting class B from class A. While providing the definitions of virtual functions, I am trying to access the private members of class A (eg Ptr ).我是从 A 类公共继承 B 类。在提供虚函数的定义的同时,我试图访问 A 类的私有成员(例如 Ptr )。 However, compiler is unable to find declaration of all protected members of class A. What is going wrong here?但是,编译器无法找到类 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;
        }

All the private members were needed to referred with template definition of the parent.所有私有成员都需要使用父级的模板定义来引用。 eg first would change to linkedListType::first;例如 first 将更改为 linkedListType::first;

This is because of some compiler limitations to use the template library.这是因为使用模板库时存在一些编译器限制。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 为什么派生类无法访问受保护的基类成员? - Why the derived class cannot access protected base class members? 从派生类访问基类的受保护数据成员 - Access protected data members of the base class from the derived class 我们可以在派生类中访问基类的受保护成员函数吗? - Can we access protected member functions of base class in derived class? 使用指向派生类中基类的指针访问受基类保护的成员 - Access base class protected members using pointers to base class in derived class 为什么我不能使用受保护/私有继承在派生实例中访问基类的受保护成员? - Why can't I access protected members of base class in derived instances using protected/private inheritance? 从公共派生类访问受保护的成员 - Access protected members from a public derived class 如何访问派生类中的受保护成员? - How to access protected members in a derived class? 使从一个基类派生的类能够使用继承的受保护成员 - Make classes derived from one base class able to use inherited protected members 如何通过将对象传递给另一个派生 class 来访问其派生 class 的基础 class 的受保护数据成员 - How to access protected data members of base class of its derived class by passing objects to another derive class 受保护的继承是否允许派生类访问其基类的私有成员? - Does protected inheritance allow the derived class access the private members of its base class?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM