求大神解决 no appropriate default constructor available

baidu_33416654 2017-05-07 10:47:18
多线程安全队列:
#include <stdio.h>
#include <stdlib.h>
#include<windows.h>
#include <time.h>
#include<process.h>
#include<malloc.h>
#define random(x) (rand() % x)
#define cr 1 //1标识为插入
#define sc 0 //0标识为删除

volatile int readcount=0; //读者数目

const int qarea=10000; //队列大小随机数

const int sum=1000; //线程运行总次数

const int sarea=500;//元素范围随机数

int th=0; //初始化当前线程总数
int th_cz=1; //初始化当前查找线程总数
int th_cr=1; //初始化当前插入线程总数
int th_sc=1; //初始化当前删除线程总数

HANDLE hMutex;//控制读者数量readcount的互斥访问量

HANDLE rMutex;//控制读写互斥,写写互斥的信号量
typedef struct node
{
int data;
struct node* next;

}node,*QNode;

typedef struct
{
QNode front;
QNode rear;
int Length;

}LinkQueue;



struct Readarg
{


LinkQueue &Queue; //定义队列
int e; //定义查找元素


};
//定义写者传参结构体


struct Writearg
{
LinkQueue &Queue; //定义队列
int n;
int e; //定义插入元素
int Flag; //定义传入标示符(cr执行插入操作,sc执行删除操作)

};



void InitQueue(LinkQueue &Q);
void EnQueue(LinkQueue &Q,int e);
void DeQueue(LinkQueue &Q,int &e);
void Clear(LinkQueue &Q);
QNode Find(LinkQueue &Q,int e) ;
void Print(LinkQueue &Q);
unsigned __stdcall ReaderThread(void *arg); //读者线程(查找)
unsigned __stdcall WriterThread(void *arg); //写者线程(包括插入和删除)


void InitQueue(LinkQueue &Q)
{
int e,c,i;
QNode p;
Q.front=Q.rear=(QNode)malloc(sizeof(node));
if(!Q.front)
{
printf("存储分配失败!\n");
exit(0);
}
Q.front->next=NULL;
Q.Length=0;
printf("队列初始化完毕!\n");


srand((unsigned int)time(NULL));
c=random(qarea);

if(!c){
printf("队列创建失败!\n");
exit(0);// 异常处理,如果用户未输入结点个数则跳出该段代码。
}
else{
p=(QNode)malloc(sizeof(node)); //p动态分配存储空间
if(!p){
printf("结点p动态分配内存失败!\n");
exit(0); //异常处理,如果节点p动态分配内存失败则跳出该段代码。
}
}
e=random(sarea);//变量e取范围0~searea内的随机整数
p->data=e;
p->next=NULL;
Q.rear->next=p;
Q.rear=p;
if(Q.front->next==NULL)
Q.front->next=p;
Q.Length++;

for(i=1;i<c;i++)
{
p=(QNode)malloc(sizeof(node));//p动态分配存储空间
if(!p){
printf("结点p动态分配内存失败!\n");
exit(0); // 异常处理,如果用户未输入结点个数则跳出该段代码。
}
e=random(sarea);//变量e取范围0~sarea内的随机整数
p->data=e;
p->next=NULL;
Q.rear->next=p;
Q.rear=p;
if(Q.front->next==NULL)
Q.front->next=p;
Q.Length++;
}

}


void EnQueue(LinkQueue &Q,int e)
{
node *p=new node;
srand((unsigned int)time(NULL));
p=(QNode)malloc(sizeof(node));
if(!p)
{
printf("存储分配失败!\n");
exit(0);

}
e=random(sarea);
printf("插入队列元素: %d",e );
printf("\n");
p->data=e;
p->next=NULL;
Q.rear->next=p;
Q.rear=p;
if(Q.front->next==NULL)
Q.front->next=p;
printf("插入成功!\n ");
Q.Length++;

}



void DeQueue(LinkQueue &Q,int &e)
{
QNode p;

if(Q.front==Q.rear)
printf("队列为空!");
p=Q.front->next;
e=p->data;
Q.front->next=p->next;
if(Q.rear==p)
Q.rear=Q.front;
free(p);
Q.Length--;
printf("删除成功!\n");


}

void Clear(LinkQueue &Q)
{
QNode p,q;
Q.rear=Q.front;
p=Q.front->next;
Q.front->next=NULL;//只留下头结点
while(p)
{
q=p;
p=p->next;
free(q);
}
printf("清空成功!");
}

QNode Find(LinkQueue &Q,int e)
{
QNode p;
p=Q.front->next;
if(Q.front==Q.rear)
printf("队列为空!找不到该元素!");
else
{


while(p->data!=e&&p->next!=NULL)
{

p = p->next;
}
if(p->data==e)
{
printf("元素%d已找到!\n",e);
return p;

}
else
{
printf("元素%d未找到!\n",e);

}
}
}

void Print(LinkQueue &Q)
{

QNode p;
p=Q.front->next;
if(Q.front==Q.rear)
printf("队列为空!");
else
{
int k;
while(p!=NULL)
{

printf("%d ",p->data);

p=p->next;

}
}
printf("长度为:%d",Q.Length);

}

//读者线程(查找)
unsigned __stdcall ReaderThread(void *arg){
Readarg *RA;
RA=(Readarg*)arg;

int e;

WaitForSingleObject(hMutex,-1);//等待互斥量信号
readcount++;
if(readcount==1){
WaitForSingleObject(rMutex,INFINITE);//等待信号量信号
}
ReleaseMutex(hMutex);//释放互斥量信号
e=RA->e;
printf("查找操作:读者%d开始查找%d\n",th_cz,e);
Find(RA->Queue,e);
th_cz++; //执行完一遍查找读者数加1

WaitForSingleObject(hMutex,-1);
readcount--;
if(readcount==0){
ReleaseSemaphore(rMutex,1,NULL);//释放信号量信号
}
ReleaseMutex(hMutex);
return 0;
}

//写者线程(包括插入和删除)
unsigned __stdcall WriterThread(void *arg){
Writearg *WA;
WA=(Writearg*)arg;

int f,e,n;
f=WA->Flag;
n=WA->n;
e=WA->e;

if(f){
WaitForSingleObject(rMutex,INFINITE);
printf("插入操作:写者%d开始在队尾插入元素%d\n",th_cr,e);
EnQueue(WA->Queue,e);
th_cr++;//执行完一遍插入写者数加1
Print(WA->Queue);
ReleaseSemaphore(rMutex,1,NULL);
}else{
WaitForSingleObject(rMutex,INFINITE);
printf("删除操作:写者%d开始删除头结点\n",th_sc);
DeQueue(WA->Queue,n);
th_sc++;//执行完一遍删除写者数加1
Print(WA->Queue);
ReleaseSemaphore(rMutex,1,NULL);
}
return 0;
}



int main(){

char sr;
printf("**************************\n");
printf(" 1.读者优先\n");
printf(" 2.退出窗口\n");
printf("**************************\n");
printf("请输入你的选择(1或者2):");
do{
sr=(char)getchar();
}while(sr!='1'&&sr!='2');
//system("cls");
if(sr=='2')
return 0;
else{

HANDLE hThread[sum];
unsigned threadID[sum];

int i,j,m;

LinkQueue Q;

InitQueue(Q);
Print(Q);

hMutex=CreateMutex(NULL,FALSE,NULL); //创建互斥量hMutex
rMutex=CreateSemaphore(NULL,1,1,NULL); //创建信号量rMutex

srand((unsigned int)time(NULL));
for(i=0;i<sum;i++){
j=random(3);//在0,1,2这三个数内随机取一个值决定该次循环执行哪一个操作(0为查找,1为插入,2为删除)
if(j==0){

Readarg *RA=new Readarg[1];
RA[0].Queue=Q;
RA[0].e=random(sarea);
//创建读者线程
hThread[i]=(HANDLE)_beginthreadex(NULL,0,ReaderThread,(void*)&RA[0],0,&threadID[i]);
}
else{

Writearg *WA=new Writearg[2];
WA[0].Queue=Q;
WA[0].e=random(sarea);
WA[0].Flag=cr;

WA[1].Queue=Q;
WA[1].e=0;
WA[1].Flag=sc;
//k=i;
//m=k%4;
if(j==1){
//创建写者线程(插入)
hThread[i]=(HANDLE)_beginthreadex(NULL,0,WriterThread,(void*)&WA[0],0,&threadID[i]);
}else
//创建写者线程(删除)
hThread[i]=(HANDLE)_beginthreadex(NULL,0,WriterThread,(void*)&WA[1],0,&threadID[i]);
}
WaitForSingleObject(hThread[i],INFINITE);//循环内wait操作,及时收回线程
}
for(i=0;i<sum;i++){
CloseHandle(hThread[i]);
}

CloseHandle(hMutex);
CloseHandle(rMutex);
th=(th_cz+th_cr+th_sc)-3; //统计总线程数
printf("当前查找读者人数为:%d;当前插入写者人数为:%d;当前删除写者人数为:%d;当前总人数为:%d\n",th_cz-1,th_cr-1,th_sc-1,th);
Clear(Q);
Print(Q);
printf("所有线程都执行完毕了。\n");
}
return 0;
}



错误:
Queue.cpp(338) : error C2512: 'Readarg' : no appropriate default constructor availabe
Queue.cpp(346) : error C2512: 'Writearg' : no appropriate default constructor available
实在不知道哪里出问题了!求各位大神指导!很急!很急!
...全文
227 1 打赏 收藏 转发到动态 举报
写回复
用AI写文章
1 条回复
切换为时间正序
请发表友善的回复…
发表回复
wang0635 2017-05-08
  • 打赏
  • 举报
回复
Readarg *RA=new Readarg[1]; 改成 Readarg *RA = (Readarg *)malloc(sizeof(Readarg) * 1); Writearg *WA=new Writearg[2]; 改成 Writearg *WA = (Writearg *)malloc(sizeof(Writearg) * 2); 或者,添加两个struct的构造函数

69,371

社区成员

发帖
与我相关
我的任务
社区描述
C语言相关问题讨论
社区管理员
  • C语言
  • 花神庙码农
  • 架构师李肯
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧