是什么使构造函数受到保护


#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)祈祷…

最新更新