错误cannot access protected member declared in class 'datastruckarray' 帮忙看一下
#include<iostream>
#include<string>
#include<algorithm>
using namespace std;
const int max_list = 10;
class node
{
public:
string entry;
int next;
};
class datastruckarray
{
public:
datastruckarray();
int size() const;
bool full() const;
bool empty() const;
void clear();
int retrieve(int position, string &x) const;
int replace (int position, const string &x );
int remove (int position, string &x);
int insert (int position, const string &x);
protected:
node Darray[max_list];
int new_node();
void delete_node(int index);
int current_position(int index) const;
int set_position(int position) const;
int available,last_used,head;
};
datastruckarray::datastruckarray()
{
head = 0;
available = -1;
last_used = -1;
Darray[head].next = -1;
}
int datastruckarray::size() const
{
int number = 0, previous = head;
for(; previous != -1;number++ )
{
previous = Darray[previous].next;
}
return number;
}
bool datastruckarray::full() const
{
int number = size();
if( number == max_list)
return true;
else
return false;
}
bool datastruckarray::empty() const
{
if(Darray[head].entry =="")
return true;
if( Darray[head].next == -1 )
{
if(Darray[head].entry =="")
return true;
else
return false;
}
else
return false;
}
void datastruckarray::clear()
{
available = Darray[head].next;
Darray[head].next = -1;
}
int datastruckarray::new_node()
{
if( full() )
return -1;
else
{
if(available == -1)
{
int i=0;
for(; (i<max_list -1) &&Darray[i].entry!="";i++)
{
}
return i;
}
else
return available;
}
}
int datastruckarray::current_position(int index) const
{
int number =0, previous =head;
for(; previous != index;)
{
previous = Darray[previous].next;
number++;
}
return number;
}
int datastruckarray::set_position(int position) const
{
int number =0,previous =head;;
if( position > size()-1 )
return -1;
for(;number != position ;number++)
{
previous = Darray[previous].next;
}
return previous;
}
void datastruckarray::delete_node(int index)
{
int follows, ahead, previous;
previous = current_position(index);
if( previous == 0 )
{
head = Darray[index].next;
Darray[index].next = available ;
available = index;
}
else if(Darray[previous].next == -1)
{
ahead = set_position(previous-1);
Darray[ahead].next = -1;
Darray[previous].next = available;
available = previous;
}
else
{
ahead = set_position(previous-1);
follows = Darray[previous].next;
Darray[ahead].next = follows;
Darray[previous].next = available;
available = previous;
}
if(last_used == 0)
last_used = available;
}
int datastruckarray::replace(int position, const std::string &x)
{
int previous = set_position(position);
if(previous == -1)
return -1;
else
Darray[previous].entry = x;
return 0;
}
int datastruckarray::retrieve(int position, string &x) const
{
int previous = set_position (position);
if(previous == -1)
return -1;
else
x = Darray[previous].entry;
return 0;
}
int datastruckarray::remove(int position, std::string &x)
{
int place = set_position(position);
if(place == -1)
return -1;
else
{
x = Darray[place].entry;
delete_node(place);
return 0;
}
}
int datastruckarray::insert(int position, const string &x)
{
int new_index = new_node();
if( new_index == -1 )
return -1;
if(available != last_used)
{
available = Darray[new_index].next;
}
if(position == 0)
{
Darray[head].entry = x;
}
else
{
int previous = set_position(position);
int Dsize = size();
Darray[set_position(position-1)].next = new_index;
Darray[new_index].entry = x;
if( previous == Dsize )
{
Darray[new_index].next = -1;
}
else
{
Darray[new_index].next = previous;
}
}
return 0;
}
int main()
{
datastruckarray Dlist;
Dlist.insert(0,"jan");
Dlist.insert(1,"feb");
Dlist.insert(2,"mar");
Dlist.insert(3,"app");
Dlist.insert(3,"guo");
Dlist.delete_node(1); //错误在这里
cout<<Dlist.size()<<endl;
cout<<Dlist.size()<<endl;
return 0;
}
错误是 'datastruckarray::delete_node' : cannot access protected member declared in class 'datastruckarray'
不懂怎么回事 在线等 谢谢