C++ 无法访问类:队列类模板中声明的私有成员
我必须编写代码来实现模板队列 我得到这个错误:无法访问类中声明的私有成员 在这条线上C++ 无法访问类:队列类模板中声明的私有成员,c++,templates,C++,Templates,我必须编写代码来实现模板队列 我得到这个错误:无法访问类中声明的私有成员 在这条线上 front=front->next; 这是我的代码的头文件,我从中得到错误: #include <iostream> #pragma once using namespace std; typedef int Error_code; #define SUCCESS 0 #define OVERFLOW -1 #define UNDERFLOW -2 template <cla
front=front->next;
这是我的代码的头文件,我从中得到错误:
#include <iostream>
#pragma once
using namespace std;
typedef int Error_code;
#define SUCCESS 0
#define OVERFLOW -1
#define UNDERFLOW -2
template <class T>
class Node{
T item;
Node * next;
Node(){item=0; next=NULL;}
Node(T n){item=n; next=NULL:}
};
template <class T>
class queue
{
protected:
Node<T>* front; // pointer to front of Queue
Node<T> * rear; // pointer to rear of Queue
int count; // current number of items in Queue
public:
queue();
~queue();
bool isempty(){
//return count == 0;
if(front==NULL)
return true;
else
return false;
};
bool isfull(){return false;};
Error_code serve(){
Error_code outcome = SUCCESS;
Node<T> *p;
if(isempty()){
cout<<"empty queue";
outcome=UNDERFLOW;
}
else{
p=front;
front=front->next;
delete p;
count--;
}
return outcome;
} ;
Error_code retrieve(T &item){
Error_code outcome SUCCESS;
if(isempty())
{ // front node is empty, queue is empty
//return false;
cout<<"empty queue";
outcome=UNDERFLOW;
}
return outcome;
};
Error_code append(T item){
Node<T> * n ;
n= new Node; // create node
n->item = item; // set node pointers
n->next = NULL;
if (isempty())
{
rear=front = n;
}
else
{
rear->next = n; // else place at rear
rear = n; // have rear point to new node
}
count++;
return SUCCESS;
};
};
#包括
#布拉格语一次
使用名称空间std;
typedef int错误代码;
#定义成功0
#定义溢出-1
#定义下溢-2
模板
类节点{
T项;
节点*下一步;
Node(){item=0;next=NULL;}
节点(tn){item=n;next=NULL:}
};
模板
类队列
{
受保护的:
Node*front;//指向队列前面的指针
节点*rear;//指向队列后部的指针
int count;//队列中的当前项目数
公众:
队列();
~queue();
布尔是空的{
//返回计数==0;
if(front==NULL)
返回true;
其他的
返回false;
};
bool isfull(){return false;};
错误代码serve(){
错误\代码结果=成功;
节点*p;
if(isempty()){
coutnext=n;//否则放在后面
rear=n;//使后点指向新节点
}
计数++;
回归成功;
};
};
front
是指向节点的指针,但next
是节点中的私有成员
template <class T>
class Node{
T item; // since you didn't specify access level
Node * next; // explicitly the access is private by default
Node(){item=0; next=NULL;}
Node(T n){item=n; next=NULL:}
};
将其更改为public
或重新设计程序程序中有三个语法错误:next=NULL:
应为next=NULL代码>-错误\u代码结果成功代码>应该是错误\u代码结果=成功代码>--n=新节点代码>应该是n=新节点代码>next
在Node
类中是私有的。我纠正了语法错误@DyP注意到,我将next改为public,但现在我得到了以下错误:错误5错误LNK2019:未解析的外部符号“public:u thiscall queue::~queue(void)”(??1$queue@H@@QAE@XZ)在功能_main“将其更改为公共或重新设计程序”中引用或者befriendqueue
我将其更改为public,但现在我得到了以下错误:错误5错误LNK2019:未解析的外部符号“public:u thiscall queue::~queue(void)”(??1$queue@H@@QAE@XZ)在函数中引用_main@user2908749你已经宣布,但在您的示例中未定义队列的ctor和dtor
。@DyP我是在.cpp中定义的file@DyP我将ctor和dtor的定义移到了头文件中,现在可以工作了。
front=front->next; // error