如何修复GCC错误:预期,而之前无效 [英] How to fix gcc error: expected while before void

查看:415
本文介绍了如何修复GCC错误:预期,而之前无效的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以,我编写使用pthreads的管理所有的IO一个对等网络聊天客户端,当我编译文件的gcc给我的错误

  client.c:在函数'accepted_daemon:
client.c:115:1:错误:预期,而前无效
 无效*
 ^
client.c:在函数'listen_daemon:
client.c:176:1:错误:预期',而'前'廉政'
 INT主(INT ARGC,CHAR *的argv [])
 ^

源$ C ​​$ C我的程序是

 的#include<&stdlib.h中GT;
#包括LT&;&string.h中GT;
#包括LT&;&stdint.h GT;
#包括LT&;&errno.h中GT;
#包括LT&;&ASSERT.H GT;#包括LT&;&unistd.h中GT;
#包括LT&;&pthreads.h中GT;
#包括LT&;的readline / readline.h>
#包括LT&;&error.h GT;#定义错误(S,E,...)error_at_line(S,E,__FILE__,__LINE__,__VA_ARGS__)#包括LT&; SYS / socket.h中>
#包括LT&; ARPA / inet.h>
#包括LT&; netinet / in.h中>
#包括LT&;&netdb.h中GT;#定义端口3248
#定义提示信息:接受结构
{
  INT的fd;
  结构sockaddr_in的地址;
};结构值
{
  结构体接受* ACC;
  结构值* NXT;
};pthread_mutex_t互斥= PTHREAD_MUTEX_INITIALIZER;
结构值*远程= NULL;空虚
push_remote(结构*接受ACC)
{
  调用pthread_mutex_lock(安培;互斥);
  结构值* S =的malloc(sizeof的* S);
  S-GT&; ACC = ACC;
  S-GT&; NXT =遥控;
  远程= S;
  调用pthread_mutex_unlock(安培;互斥);
}空虚
pop_remote(结构*接受ACC)
{
  调用pthread_mutex_lock(安培;互斥);
  结构值=头{NULL,远程};
  结构值* S =安培;头;
  而(S-GT&;&nxt- GT;!ACC = ACC)
    {
      S = S-> NXT;
      如果(S-GT&; NXT == NULL)
    返回;
    }
  结构值* TMP = S-> nxt-> NXT;
  免费(S-GT&; NXT);
  S-GT&; NXT = TMP;
}结构接受*
make_socket(uint32_t的s_addr)
{
  结构体接受*地址=的malloc(sizeof的*地址);
  addr-> FD =插座(PF_INET,SOCK_STREAM,0);
  如果(addr-> FD℃,)
    {
      免费(地址);
      返回NULL;
    }  addr-> addr.sin_family = AF_INET;
  addr-> addr.sin_port = htons(PORT);
  addr-> addr.sin_addr.s_addr = s_addr;  如果(连接(addr-> FD(结构sockaddr *)及addr->地址,
           sizeof的addr->地址)LT; 0)
    {
      免费(地址);
      返回NULL;
    }  返回地址;
}无效*
accepted_daemon(无效* ARG)
{
  pthread_cleanup_push(免费,ARG);
  结构接受* ARGS = ARG;
  pthread_cleanup_push(接近,args-> FD);
  push_remote(参数);
  pthread_cleanup_push(pop_remote,参数);  而(1)
    {
      炭缓冲器[100];
      ssiz​​e_t供字符=读(args-> FD,缓冲区,缓冲区的sizeof);
      如果(字符℃,)
    {
      错误(0,错误号,主机%S断开
        inet_ntop(AF_INET,阿根廷,缓冲区,缓冲区的sizeof));
      返回NULL;
    }
      写(1,缓冲区,个字符);
      写(1,\\ n的strlen(\\ n)* sizeof的(炭));
    }
  返回NULL;
}无效*
initial_connection(无效* ARG)
{
  uint32_t的主机=(uint32_t的)ARG;
  结构体接受* ACC = make_socket(ARG);
  如果(ACC == NULL)
    {
      炭缓冲器[100];
      错误(1,错误号,无法连接到主机%S
       inet_ntop(AF_INET,阿根廷,缓冲区,缓冲区的sizeof));
    }  而(1)
    {
      uint32_t的nxthost;
      阅读(袜子,和放大器; nxthost,sizeof的nxthost);
      如果(nxthost == 0)
    打破;
      线程的pthread_t;
      在pthread_create(安培;螺纹,NULL,initial_connection,(无效*)nxthost);
    }  返回accepted_daemon(ACC);
}
无效*
listen_daemon(无效* ARG)
{
  INT袜子=插座(PF_INET,SOCK_STREAM,0);
  pthread_cleanup_push(接近,袜子);
  结构sockaddr_in的地址;
  addr.sin_family = AF_INET;
  addr.sin_port = htons(PORT);
  addr.sin_addr.s_addr = INADDR_ANY;  绑定(袜子,(结构sockaddr *)及地址,sizeof的地址);
  听(袜子,5);  而(1)
    {
      结构体接受* ACC =的malloc(sizeof的* ACC);
      socklen_t的LEN;
      acc-> FD =接受(袜子,(结构sockaddr *)及acc->地址,和放大器; LEN);      调用pthread_mutex_lock(安培;互斥);
      结构值* P =遥控;
      而(P!= NULL)
    {
      写(acc-> FD,和放大器; P-> acc-> addr.sin_addr.s_addr,sizeof的(uint32_t的));
      P = P-> NXT;
    }
      调用pthread_mutex_unlock(安培;互斥);      线程的pthread_t;
      在pthread_create(安培;螺纹,NULL,accepted_daemon,(无效*)ACC);
    }
  返回NULL;
}
INT主(INT ARGC,CHAR *的argv [])
{
  断言(ARGC == 2);  结构hostent *目标= gethostbyname2(的argv [1],AF_INET);
  如果(目标== NULL)
    错误(1,错误号,主机找不到);  线程的pthread_t;
  在pthread_create(安培;螺纹,NULL,initial_connection,
          (无效*)inet_addr(靶子> h_addr));  在pthread_create(安培;螺纹,NULL listen_daemon,NULL);  在readline的=(提示)的char *;
  而(在!= NULL)
    {
      调用pthread_mutex_lock(安培;互斥);
      结构值* P =遥控;
      而(P!= NULL)
    {
      写(对GT; addr-> FD中,strlen的(中));
      P = P-> NXT;
    }
      调用pthread_mutex_unlock(安培;互斥);
      自由();
      在readline的=(提示);
    }  返回0;
}


解决方案

pthread_cleanup_push()最有可能被实现为宏引入一个开括号 {这需要一个(相应的) pthread_cleanup_pop()在相同的情况下。后者则服务于大括号} * 1

有一个看code的pre-处理器输出(和成根据人的页面,当然,头文件),你会得到启发。


* 1这种实现的,顺便说一句,是最严格的方式我见过管教C codeRS ...; - >

So I am writing a peer-to-peer chat client that uses pthreads to manage all the IO and when I compile the file gcc gives me the error

client.c: In function ‘accepted_daemon’:
client.c:115:1: error: expected ‘while’ before ‘void’
 void *
 ^
client.c: In function ‘listen_daemon’:
client.c:176:1: error: expected ‘while’ before ‘int’
 int main(int argc, char *argv[])
 ^

The source code for my program is

#include <stdlib.h>
#include <string.h>
#include <stdint.h>
#include <errno.h>
#include <assert.h>

#include <unistd.h>
#include <pthread.h>
#include <readline/readline.h>
#include <error.h>

#define error(s, e, ...) error_at_line (s, e, __FILE__, __LINE__, __VA_ARGS__)

#include <sys/socket.h>
#include <arpa/inet.h>
#include <netinet/in.h>
#include <netdb.h>

#define PORT 3248
#define PROMPT "message: "

struct accepted 
{
  int fd;
  struct sockaddr_in addr;
};

struct value
{
  struct accepted *acc;
  struct value *nxt;
};

pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER;
struct value *remote = NULL;

void
push_remote (struct accepted *acc)
{
  pthread_mutex_lock (&mutex);
  struct value *s = malloc (sizeof *s);
  s->acc = acc;
  s->nxt = remote;
  remote = s;
  pthread_mutex_unlock (&mutex);
}

void
pop_remote (struct accepted *acc)
{
  pthread_mutex_lock (&mutex);
  struct value head = { NULL, remote };
  struct value *s = &head;
  while (s->nxt->acc != acc)
    {
      s = s->nxt;
      if (s->nxt == NULL)
    return;
    }
  struct value *tmp = s->nxt->nxt;
  free (s->nxt);
  s->nxt = tmp;
} 

struct accepted *
make_socket (uint32_t s_addr)
{
  struct accepted *addr = malloc (sizeof *addr);
  addr->fd = socket (PF_INET, SOCK_STREAM, 0);
  if (addr->fd < 0)
    {
      free (addr);
      return NULL;
    }

  addr->addr.sin_family = AF_INET;
  addr->addr.sin_port = htons (PORT);
  addr->addr.sin_addr.s_addr = s_addr;

  if (connect (addr->fd, (struct sockaddr *) &addr->addr, 
           sizeof addr->addr) < 0)
    {
      free (addr);
      return NULL;
    }

  return addr;
}

void *
accepted_daemon (void *arg)
{
  pthread_cleanup_push (free, arg);
  struct accepted *args = arg;
  pthread_cleanup_push (close, args->fd);
  push_remote (args);
  pthread_cleanup_push (pop_remote, args);

  while (1)
    {
      char buffer[100];
      ssize_t chars = read (args->fd, buffer, sizeof buffer);
      if (chars < 0)
    {
      error (0, errno, "Host %s disconnected", 
        inet_ntop (AF_INET, arg, buffer, sizeof buffer));
      return NULL;
    }
      write (1, buffer, chars);
      write (1, "\n", strlen ("\n") * sizeof (char));
    }
  return NULL;
}

void *
initial_connection (void *arg)
{
  uint32_t host = (uint32_t) arg;
  struct accepted *acc = make_socket (arg);
  if (acc == NULL)
    {
      char buffer[100];
      error (1, errno, "Failed to connect to host %s", 
       inet_ntop (AF_INET, arg, buffer, sizeof buffer));
    }

  while (1)
    {
      uint32_t nxthost;
      read (sock, &nxthost, sizeof nxthost);
      if (nxthost == 0)
    break;
      pthread_t thread;
      pthread_create (&thread, NULL, initial_connection, (void *) nxthost);
    }

  return accepted_daemon (acc);
}


void *
listen_daemon (void *arg)
{
  int sock = socket (PF_INET, SOCK_STREAM, 0);
  pthread_cleanup_push (close, sock);
  struct sockaddr_in addr;
  addr.sin_family = AF_INET;
  addr.sin_port = htons (PORT);
  addr.sin_addr.s_addr = INADDR_ANY;

  bind (sock, (struct sockaddr *) &addr, sizeof addr);
  listen (sock, 5);

  while (1)
    {
      struct accepted *acc = malloc (sizeof *acc);
      socklen_t len;
      acc->fd = accept (sock, (struct sockaddr *) &acc->addr, &len);

      pthread_mutex_lock (&mutex);
      struct value *p = remote;
      while (p != NULL)
    {
      write (acc->fd, &p->acc->addr.sin_addr.s_addr, sizeof (uint32_t));
      p = p->nxt;
    }
      pthread_mutex_unlock (&mutex);

      pthread_t thread;
      pthread_create (&thread, NULL, accepted_daemon, (void *) acc);
    }
  return NULL;
}


int main(int argc, char *argv[])
{
  assert (argc == 2);

  struct hostent *target = gethostbyname2 (argv[1], AF_INET);
  if (target == NULL)
    error (1, errno, "Host could not be found");

  pthread_t thread;
  pthread_create (&thread, NULL, initial_connection, 
          (void *) inet_addr (target->h_addr));

  pthread_create (&thread, NULL, listen_daemon, NULL);

  char *in = readline (PROMPT);
  while (in != NULL)
    {
      pthread_mutex_lock (&mutex);
      struct value *p = remote;
      while (p != NULL)
    {
      write (p->addr->fd, in, strlen (in));
      p = p->nxt;
    }
      pthread_mutex_unlock (&mutex);
      free (in);
      in = readline (PROMPT);
    }

  return 0;
}

解决方案

pthread_cleanup_push() most likely is implemented as a macro introducing an open brace { which expects a (corresponding) pthread_cleanup_pop() in the same context. The latter then serves the closing brace }. *1

Have a look at the pre-processor output of the code (and into the according man-pages and header files of course) and you'll get enlightened.


*1 This kind of implementation, BTW, is the most rigorous way I ever saw to discipline C coders ... ;->

这篇关于如何修复GCC错误:预期,而之前无效的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆