是什么使构造函数受到保护
What is making my constructor protected?
#ifndef SLIST_H
#define SLIST_H
#include "llist.h"
using namespace std;
class slist:public llist{
public:
slist();
int search(el_t Key);
void replace(el_t Elem, int I);
};
#endif
这是我刚创建的新类它在list。h
中包含的所有继承函数的基础上,提供了搜索和替换函数In my main…
#include "slist.h"
#include <iostream>
using namespace std;
int main(){
slist list;
list.addFront(4);
cout<<list.search(4);
}
我试图调用addfront(),这是一个公共函数在列表类。然后我想调用search(),它是list类的继承公共函数。g++给了我一些我不理解的错误
slist.h: In function âint main()â:
slist.h:10: error: âslist::slist()â is protected
main.cpp:7: error: within this context
slist()是受保护的?为什么?我把它放在public:
还有this上下文是怎么回事,我猜我只是把整个继承都做错了。任何帮助将不胜感激!
编辑:这是list类,如果它有帮助的话#ifndef LIST_H
#define LIST_H
#include <iostream>
using namespace std;
class llist{
protected:
typedef int el_t;
el_t total;
struct Node{
int Elem;
Node *Next;
};
Node *Front;
Node *Rear;
Node * Curr;
public:
class Overflow{};
class Underflow{};
class Range{};
llist();
~llist();
bool isEmpty();
void displayAll();
void addRear(el_t NewNum);
void deleteFront(el_t& OldNum);
void addFront(el_t NewNum);
void deleteRear(el_t& OldNum);
void deleteIth(int I, el_t& OldNum);
void addbeforeIth(int I, el_t newNum);
class Overflow;
};
#endif
这是list.cpp,只粘贴了相关函数
#include "llist.h"
#include <iostream>
using namespace std;
int total=0;
llist::llist(){
Front=NULL;
Rear=NULL;
total=0;
}
llist::~llist(){
while(Front!=NULL){
int z;
deleteFront(z);
}
}
bool llist::isEmpty(){
if(Front==NULL){
return true;
}
return false;
}
void llist::displayAll(){
Curr=Front;
if(isEmpty()){
cout<<"[ empty ]"<<endl;
}else{
while(Curr!=NULL){
cout<<"curr != NuL"<<endl;
cout<<Curr->Elem<<endl;
Curr=Curr->Next;
}
}
}
void llist::addFront(el_t NewNum){
if(isEmpty()){
Node *x=new Node;
x->Next=Front;
Rear=Front;
Front=x;
Front->Elem=NewNum;
}else{
Node *x=new Node;
x->Next=Front;
Front=x;
Front->Elem=NewNum;
++total;
}
}
老实说,我看不出问题,但不是每个编译器都是标准兼容的,所以我会尝试以下:
1)重命名你的类-如果它工作,这意味着它是一个,因为命名冲突。
2)删除using指令。
3)移除继承。如果这之后还能行…你真的需要改变编译器
4)在你的类声明之前尝试#undef public
。如果这之后还能行…嗯,有人要和经理谈谈。
5)祈祷…
相关文章:
- C++:为什么无法在派生类中访问受保护的构造函数?
- 在父类中公开受保护的构造函数
- 为什么继承的受保护构造函数不能公开?
- 将派生类构造函数参数传递给受保护的成员
- 在派生类的构造函数初始化中无法访问受保护的函数
- 尝试从基类访问受保护的构造函数时出现错误 C2248
- 继承公共/受保护/私有构造函数
- 更改了C 17中受保护构造函数的规则
- C 非堆工厂对象创建具有受保护的构造函数和复制构造函数
- 在C 中的多级虚拟继承中受保护的构造函数
- 将std :: shared_ptr与受保护的构造函数 destructor一起使用
- protected是否保护基类构造函数或继承的类构造函数
- 检测(可能是抽象的)基类的受保护构造函数
- 构造函数中的C++保护声明
- 初始化类构造函数中的受保护常量
- 模板化转换构造函数无法访问受保护的数据成员
- 如果基类构造函数受到保护,为什么我们不能在派生类函数中创建基类对象
- 访问基类的受保护构造函数
- 单例保护构造函数vs私有构造函数
- 派生类中的静态方法可以调用c++中的受保护构造函数吗?