无法访问派生类中的基本受保护成员!(在虚函数中)
Not able to access base protected members in derived class ! ( in virtual functions )
我有一个抽象类,例如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;
}
所有私有成员都需要使用父级的模板定义进行引用。例如 第一个将更改为链接列表类型::第一个;
这是因为编译器在使用模板库时存在一些限制。
相关文章:
- 对RValue对象调用的LValue ref限定成员函数
- 为什么使用 "this" 指针调用派生成员函数?
- 将公共但非静态的成员函数与ALGLIB集成
- 使用指向成员的指针将成员函数作为参数传递
- 将重载的成员函数传递给函数模板
- 我不小心调用了一个没有自己类对象的成员函数.但这是怎么回事呢
- 如何在C++中使用非静态成员函数作为回调函数
- C++错误C2600:无法定义编译器生成的特殊成员函数(必须首先在类中声明)
- 关联容器的下界复杂性:成员函数与非成员函数
- 在 C++ 中用派生类型重写成员函数
- 链表的泛型函数remove()与成员函数remove)
- 如何将lambda作为模板类的成员函数参数
- constexpr构造函数需要常量成员函数时出现问题
- 将自由函数绑定为类成员函数
- 区分非成员函数和头文件中的成员函数
- 如何从子成员函数修改父公共成员变量
- 保留对其他类的成员函数的引用
- 在运算符重载定义中使用成员函数(const错误)
- 内联如何影响模块接口中的成员函数
- 将成员函数指针作为参数传递给模板方法