AirCoder
AirCoder

Reputation: 5

Conflicting types for error

I'm working on a Priority queue with an array implementation. Everything seems to work fine but I get this error: conflicting types for 'remove' I've declared the function in its header file, I've included the header file but the compiler stil complains. I think the problem resides somewhere else.

Here is 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;
    }

Any thoughts?

Upvotes: 0

Views: 3956

Answers (3)

Michael Aaron Safyan
Michael Aaron Safyan

Reputation: 95489

To echo what everyone else has said, there is a standard function. But to add to this, in C, you should always prefix your functions with the name of your library and of the type that it operates on to avoid these sorts of issues. For example, it would be better to name your function something like:

mylibrary_pqueue_remove

This would avoid naming clashes with the standard library and with other peoples' code.

Upvotes: 2

md5
md5

Reputation: 23699

remove is a reserved identifier, you can't use it in your program, as long as you include <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.

Upvotes: 1

rid
rid

Reputation: 63442

int remove(const char *path) is a standard function. You need to choose a different name for yours.

Upvotes: 2

Related Questions