好的,首先我将从一些背景开始。我正在为一个涉及客户端和服务器的类项目工作。它们是两个独立的进程,通过我们所说的请求通道相互通信。
客户端收集有关请求数量、请求通道缓冲区大小以及发送请求的工作线程数(分别为 -n、-b 和 -w)的命令行参数。
用户想要多少个工作线程就是我必须在客户端和服务器之间创建多少个请求通道。
RequestChannel *channel;
int main(int argc, char * argv[])
{
char *n=NULL, *b=NULL, *w=NULL;
int num, buf, workers;
char optchar=0;
while((optchar=getopt(argc,argv,"n:b:w:"))!=-1)
switch(optchar)
{
case 'n':
n=optarg;
break;
case 'b':
b=optarg;
break;
case 'w':
w=optarg;
break;
case '?':
break;
default:
abort();
}
num=atoi(n);
buf=atoi(b);
workers=atoi(w);
channel=malloc(workers*sizeof(RequestChannel));
cout << "CLIENT STARTED:" << endl;
pid_t child_pid;
child_pid = fork();
if(child_pid==0)
{
execv("dataserver", argv);
}
else
{
cout << "Establishing control channel... " << flush;
RequestChannel chan("control", RequestChannel::CLIENT_SIDE);
cout << "done." << endl;
for(int i=0;i<workers;i++)
RequestChannel channel[i](chan.send_request("newthread"), RequestChannel::CLIENT_SIDE);
}
}
我的malloc
行出现编译错误,我不知道问题是什么。我只想能够访问每个RequestChannel
like channel[i]
。
现在的方式,我收到一条错误消息
void*
从到 的无效转换RequestChannel*
当我替换sizeof(RequestChannel)
with 时sizeof(*RequestChannel)
,我收到一条错误消息
')' 标记之前的预期主表达式。