2012-11-11 3 views
0

배열 구현으로 우선 순위 대기열에서 작업하고 있습니다. 모든 것이 잘 작동하는 것처럼 보이지만 다음과 같은 오류가 발생합니다. 'remove'에 대한 충돌 유형 헤더 파일에 함수를 선언했지만 헤더 파일은 포함했지만 컴파일러는 불평합니다. 나는 문제가 다른 곳에 있다고 생각한다. 여기오류의 충돌 유형

는 pqueue.h입니다 :

#ifndef PQUEUE_H 
    #define PQUEUE_H 
    //--------------- 

    #define HIGHP 0 
    #define MEDP 1 
    #define LOWP 2 
    #define MAXEL 10 

    #include <stddef.h> 

    typedef struct message { 
     char data[100]; 
     int priority; 
    } message; 

    typedef struct pQueue { 
     struct message messages[10]; 
     int rear; 
     int front; 
     int size; 
    } pQueue; 

    void initPQueue(pQueue *pq); 
    void add(pQueue *pq, char *data, int pri); 
    char* remove(struct pQueue *pq); // Error: conflicting types for: 'remove' 
    int isEmpty(pQueue *pq); 

    #endif 

pqueue.c :

#include "pqueue.h" 
    #include <string.h> 

    void initPQueue(pQueue *pq) { 
     pq->front = 0; 
     pq->rear = 0; 
     pq->size = 0; 
    } 

    void add(pQueue *pq, char *data, int pri) { 
     if (pq->size > MAXEL) { 
      return; // NOTE: data is lost 
     } 
     message m; 
     strcpy(m.data, data); 
     m.priority = pri; 

     if (isEmpty(pq)) { 
      pq->messages[pq->rear] = m; 
      pq->rear = (pq->rear % (MAXEL - 1)) + 1; 
      return; // done 
     } 

     /**TODO: NEEDS REPAIR**/ 
     int i = 0; 
     int j = 0; 
     for (; i < pq->rear; i = (i % (MAXEL - 1)) + 1) { 
      if (m.priority > pq->messages[i].priority) { 
       // found element with higher or equal priority 
       for (j = pq->rear - 1; j >= i; j = (j % (MAXEL - 1)) - 1) { 
        pq->messages[j] = pq->messages[j - 1]; 
       } 
        break; 
      } 
     } 
     pq->messages[i] = m; 
     /****/ 

     pq->size++; 
    } 

    char* remove(struct pQueue *pq) { 
     if (isEmpty(pq)) { 
      return NULL ; 
     } 
     pq->size--; 
     return pq->messages[pq->front].data; 
    } 

    int isEmpty(pQueue *pq) { 
     if (!pq->size) 
      return 1; 

     return 0; 
    } 

어떤 생각이?

답변

2

int remove(const char *path)은 표준 기능입니다. 다른 이름을 선택해야합니다.

+0

고마워요! 나는 그것을 몰랐다. – AirCoder

1

는 예약 된 식별자이므로 <stdio.h>을 포함하는 한 프로그램에서 사용할 수 없습니다.

7.21.4.1 The remove function

#include <stdio.h>
int remove(const char *filename) ;

The remove function causes the file whose name is the string pointed to by filename to be no longer accessible by that name. A subsequent attempt to open that file using that name will fail, unless it is created anew. If the file is open, the behavior of the remove function is implementation-defined.

1

다른 사람들의 의견을 반영하여 표준 기능이 있습니다. 그러나 C에이 기능을 추가하려면 라이브러리의 이름과 이러한 유형의 문제를 피하기 위해 작동하는 유형의 접두사를 항상 앞에 붙여야합니다.

mylibrary_pqueue_remove 

이 표준 라이브러리와 다른 사람들의 코드와 충돌을 명명 피할 것이다 : 예를 들어, 같은 함수 뭔가 이름을 더 잘 될 것이다.