新手error C2679: binary '=' : no operator defined which takes a right-hand oper
新手求助:error C2679: binary '=' : no operator defined which takes a right-hand oper
运行时候出现这个错误:应该怎么改?为什么DelQ出队列中写上x=-1;返回值就出现错误?
error C2679: binary '=' : no operator defined which takes a right-hand operand of type 'const int' (or there is no acceptable conversion)
#include<iostream>
#include<iomanip>
#include<string>
//#include<conio.h>
#define MAXSIZE 20
using namespace std;
struct ElemType
{
char name[10];
int num;
};
struct quenode
{
ElemType data;
quenode *next;
};
class LsQueue
{
private:
quenode *front,*rear;
public:
LsQueue();
~LsQueue();
int IsEmpty();
void Display();
void AddQ(ElemType x);
ElemType DelQ();
};
LsQueue::LsQueue()
{
quenode *p;
p=new quenode;
p->next=NULL;
front=rear=p;
}
void LsQueue::AddQ(ElemType x)
{
quenode *s;
s=new quenode;
s->data=x;
s->next=NULL;
rear->next=s;
rear=s;
}
ElemType LsQueue::DelQ()
{
ElemType x;quenode *p;
if(front==rear)
{cout<<"\n队列为空!"<<endl;x=-1; }!!!为什么这里写上x=-1会提示错误!???
else{
p=front->next;
front->next=p->next;
if(p->next==NULL)rear=front;
x=p->data;
delete p;
}
return x;
}
int LsQueue::IsEmpty()
{
if(front==rear)return 1;
else return 0;
}
LsQueue::~LsQueue()
{
quenode *p;
p=front->next;
while(p!=rear)
{
front->next=p->next;
delete p;
p=front->next;
}
delete rear;
delete front;
rear=front=NULL;
}
void LsQueue::Display()
{
quenode *p;
p=front->next;
while(p!=NULL)
{
cout<<setw(5)<<p->data.num;
cout<<setw(10)<<p->data.name<<endl;
p=p->next;
}
cout<<"OUT End!"<<endl;
}
int main()
{
int i,n=10;
ElemType e,x;
LsQueue Q;
for(i=1;i<=n;i++)
{
x.num=i;
cout<<"\ni="<<i<<"输入姓名:";
cin>>x.name;
Q.AddQ(x);
}
Q.Display();
------解决方案--------------------
这里x是ElemType型的变量,你写成x.num=-1就不会错了
------解决方案--------------------
改成x.num=-1
运行时候出现这个错误:应该怎么改?为什么DelQ出队列中写上x=-1;返回值就出现错误?
error C2679: binary '=' : no operator defined which takes a right-hand operand of type 'const int' (or there is no acceptable conversion)
#include<iostream>
#include<iomanip>
#include<string>
//#include<conio.h>
#define MAXSIZE 20
using namespace std;
struct ElemType
{
char name[10];
int num;
};
struct quenode
{
ElemType data;
quenode *next;
};
class LsQueue
{
private:
quenode *front,*rear;
public:
LsQueue();
~LsQueue();
int IsEmpty();
void Display();
void AddQ(ElemType x);
ElemType DelQ();
};
LsQueue::LsQueue()
{
quenode *p;
p=new quenode;
p->next=NULL;
front=rear=p;
}
void LsQueue::AddQ(ElemType x)
{
quenode *s;
s=new quenode;
s->data=x;
s->next=NULL;
rear->next=s;
rear=s;
}
ElemType LsQueue::DelQ()
{
ElemType x;quenode *p;
if(front==rear)
{cout<<"\n队列为空!"<<endl;x=-1; }!!!为什么这里写上x=-1会提示错误!???
else{
p=front->next;
front->next=p->next;
if(p->next==NULL)rear=front;
x=p->data;
delete p;
}
return x;
}
int LsQueue::IsEmpty()
{
if(front==rear)return 1;
else return 0;
}
LsQueue::~LsQueue()
{
quenode *p;
p=front->next;
while(p!=rear)
{
front->next=p->next;
delete p;
p=front->next;
}
delete rear;
delete front;
rear=front=NULL;
}
void LsQueue::Display()
{
quenode *p;
p=front->next;
while(p!=NULL)
{
cout<<setw(5)<<p->data.num;
cout<<setw(10)<<p->data.name<<endl;
p=p->next;
}
cout<<"OUT End!"<<endl;
}
int main()
{
int i,n=10;
ElemType e,x;
LsQueue Q;
for(i=1;i<=n;i++)
{
x.num=i;
cout<<"\ni="<<i<<"输入姓名:";
cin>>x.name;
Q.AddQ(x);
}
Q.Display();
------解决方案--------------------
这里x是ElemType型的变量,你写成x.num=-1就不会错了
------解决方案--------------------
改成x.num=-1