C ++编译错误消息

时间:2017-03-23 03:46:17

标签: c++

所以我有这个代码,我必须编辑一下,但是当我尝试使用mobaxterm时代码本身不会编译,因为我对C ++没有太多经验,我希望你们能帮忙。这是代码:

//critical_example2.c
#include <sys/ipc.h>
#include <sys/sem.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>


#include "se207_sems.h"

int main(int argc, char argv[]){
  //Use our source file as the "key"
  int id=se207_semget("critical_example2.c",1);

  int pid=fork();  
  if(pid){
    //P1
    while(1){
      se207_wait(id);
      printf("In critical section P1 ... \n");
      rsleep();
      printf("Ending critical section P1 ... \n");
      se207_signal(id);
    }
  }else{
    //P2
    while(1){
      se207_wait(id);
      printf("In critical section P2 ... \n");
     rsleep();
      printf("Ending critical section P2 ... \n");
      se207_signal(id);
    }

  }

}

这是我得到的错误:

toneve@hvs-its-lnx01:~$ gcc critical_example2.c
In file included from critical_example2.c:9:0:
se207_sems.h: In function ‘se207_wait’:
se207_sems.h:91:6: warning: type of ‘id’ defaults to ‘int’ [-Wimplicit-int]
 void se207_wait(id){
  ^
se207_sems.h: In function ‘se207_signal’:
se207_sems.h:95:6: warning: type of ‘id’ defaults to ‘int’ [-Wimplicit-int]
 void se207_signal(id){

这可能是有问题的代码:

#include <sys/ipc.h>
#include <sys/sem.h>
#include <stdio.h>
#include <stdlib.h>

void rsleep(){
  //Random sleep function.  Comes in handy demoing stuff.
  int stime=2+(rand()/(float)(RAND_MAX))*4;
  printf("Sleeping for %d secs\n",stime);
  sleep(stime);
}


int se207_semget(char* path, int val){
  //Very simple semaphore "getting", 
  //always uses 1 as the project ID
  //takes path to file and initial value of semaphore

  int id; /* Number by which the semaphore 
         is known within a program */


  union semun {
    int val;
    struct semid_ds *buf;
    ushort * array;
  } argument;

  argument.val = val;

  /* Create the semaphore with external key from 
     ftok if it doesn't already 
     exist. Give permissions to the world. */

  id = semget(ftok(path,1), 1, 0666 | IPC_CREAT);

  /* Always check system returns. */

  if(id < 0)
    {
      fprintf(stderr, "Unable to obtain semaphore.\n");
      exit(0);
    }

  /* Set the value of the number 0 semaphore in semaphore array # id
     to the value "val". */

  if( semctl(id, 0, SETVAL, argument) < 0)
      fprintf( stderr, "Cannot set semaphore value.\n");
  else
    fprintf(stderr, "Semaphore %d initialized with path '%s'.\n", 
          ftok(path,1),path);
  return id;
}

void se207_semop(int id,int val){


  struct sembuf operations[1];
  int retval; /* Return value from semop() */

  //simple wait on semaphore
  operations[0].sem_num = 0;
  /* Which operation? Subtract 1 from semaphore value to wait, add to
     signal */
  operations[0].sem_op = val;
  operations[0].sem_flg = 0;

  retval = semop(id, operations, 1);

}


int void se207_wait(id){
  se207_semop(id,-1);
}

int void se207_signal(id){
  se207_semop(id,1);
}

1 个答案:

答案 0 :(得分:2)

从se207_wait和se207_signal函数中删除int,并在参数列表中将id声明为int。

void se207_wait(int id){
  se207_semop(id,-1);
}

void se207_signal(int id){
  se207_semop(id,1);
}