943,602 Members | Top Members by Rank

Ad:
  • C Discussion Thread
  • Unsolved
  • Views: 3189
  • C RSS
Sep 17th, 2009
0

Previous declaration of ___ was here

Expand Post »
Hi, for an assignment I have to do, we have to made a Stack and Queue, and read data from a file and add to queue and push onto the stack. Well, I made an array implementation of a stack that works fine, but the array implementation of the Queue is really giving me problems. I keep getting, especially surrounding my remove subroutine, a "error: conflicting types for remove" and "previous declaration of 'remove' was here" errors.

Any help would be appreciated.

  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include "queueADT.h"
  4.  
  5. #define QUEUE_SIZE 100
  6.  
  7. struct queue_type {
  8. int contents[QUEUE_SIZE];
  9. int cursor;
  10. int size;
  11. };
  12.  
  13. static void terminate(char *message)
  14. {
  15. printf("%s\n", message);
  16. exit(EXIT_FAILURE);
  17. }
  18.  
  19. Queue create()
  20. {
  21. Queue q;
  22. q = malloc(sizeof(struct queue_type));
  23. if(q == NULL)
  24. terminate("Error in create: Queue could not be created.");
  25. q->cursor = 0;
  26. q->size = 0; //Size is zero because no elements have been added yet
  27.  
  28. return q;
  29. }
  30.  
  31. void destroy(Queue q)
  32. {
  33. free(q);
  34. }
  35.  
  36. bool is_empty(Queue q)
  37. {
  38. return q->size == 0;
  39. }
  40.  
  41. bool is_full(Queue q)
  42. {
  43. return q->size == QUEUE_SIZE;
  44. }
  45.  
  46. void add(Queue q, int i) //Adds an element to the end of the queue
  47. {
  48. int last = get_last(q); //finds last occupied spot in the queue
  49.  
  50. while(q->cursor<=QUEUE_SIZE) //Makes sure you dont go out of bounds
  51. q->cursor++;
  52. q->contents[q->cursor]=i;
  53. q->size++;
  54. }
  55.  
  56. int remove(Queue q) //Removes the first element from the queue
  57. {
  58. get_first(q); //MAKE SURE IT MOVES THE CURSOR!!!!!
  59. int removed = q->contents[0]; //Saves the item to return it
  60.  
  61.  
  62.  
  63. q->cursor++; //Prepares to move the element
  64. while(q->cursor<q->size) //Should shift each element one to the left without
  65. { //going out of bounds
  66. q->contents[q->cursor] = q->contents[q->cursor-1];
  67. q->cursor++;
  68. }
  69. q->size--;
  70.  
  71. return removed;
  72. }
  73.  
  74. void get_first(Queue q) //Moves the cursor to zero since you already know
  75. { //where the first item is
  76. q->cursor=0;
  77. }
  78.  
  79. int get_next(Queue q) //returns index of the next item in the queue and moves the cursor there
  80. {
  81. q->cursor++;
  82. return q->cursor;
  83. }
  84.  
  85. int get_last(Queue q) //finds the last element, returns its index, and moves the cursor there
  86. {
  87. q->cursor = q->size-1;
  88. return q->cursor;
  89. }
  90.  
  91. int main()
  92. {
  93. Queue q1;
  94.  
  95. q1 = create();
  96.  
  97. /* add(q1, 10);
  98. add(q1, 20);
  99.  
  100. remove(q1); */
  101.  
  102. return 0;
  103. }

I dont know how to tag my header properly, the code tags arent working... so I guess Ill just paste it in bold letters... sorry

/*queueADT.h*/

#ifndef QUEUEADT_H
#define QUEUEADT_H

#include <stdbool.h>

typedef struct queue_type *Queue;

Queue create();
void destroy(Queue q);
bool is_empty(Queue q);
bool is_full(Queue q);
void add(Queue q, int i);
int remove(Queue q);
void get_first(Queue q);
int get_next(Queue q);
int get_last(Queue q);


#endif
Similar Threads
Reputation Points: 10
Solved Threads: 0
Light Poster
TGeorge824 is offline Offline
25 posts
since Sep 2009
Sep 17th, 2009
0

Re: Previous declaration of ___ was here

> typedef struct queue_type *Queue;
This should come AFTER your actual struct declaration.
Team Colleague
Reputation Points: 5862
Solved Threads: 950
Posting Sage
Salem is offline Offline
7,164 posts
since Dec 2005
Sep 17th, 2009
1

Re: Previous declaration of ___ was here

remove is the name of a standard library function, choose another name.
Team Colleague
Reputation Points: 2780
Solved Threads: 312
long time no c
Dave Sinkula is offline Offline
4,790 posts
since Apr 2004

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C Forum Timeline: Pointers and nested structures
Next Thread in C Forum Timeline: Convert Decimal to Radix





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC