Home > Blockchain >  C Queue I am getting thousands of errors even on line that doesnt exist on the code
C Queue I am getting thousands of errors even on line that doesnt exist on the code

Time:10-29


#define size 4
#include <iostream>
using namespace std;
class queue {
    int array[size];
    int rear;
    int front;

public:

    queue() {
        front = 0;
        rear = 0;
    }
    void enqueue(int val);
    void dequeue();
};
void queue :: enqueue(int val) {
    if (rear = size) {
        cout << "sorry our queue is full " << endl;
    }
    else {
        array[rear] = val;
        rear  ;
    }
}
void queue :: dequeue() {
    if (front = rear) 
    {
        cout << "the stack is empty" << endl;
    }
    else {
        cout << "our queued element is that" << array[front] << endl;
        front  ;
    }}
int main() {

    queue bro;
    bro.enqueue(4);
    bro.enqueue(5);
    bro.enqueue(3);
    bro.enqueue(6);
    bro.dequeue();
    bro.dequeue();
    bro.dequeue();

}

I was writing the code and got a bunch of errors on this queue enqueue and dequeue array. The errors say corecrt_wio.h. Some of the errors say error on line that I didn't even write so it is really confusing.

CodePudding user response:

Writing

#define size 4

at the top of your file is insane. Never do this. There are good reasons why macro constants are discouraged in favour of something like

constexpr size_t size = 4;

and one of then is that you've replaced every token "size" in the standard iostream library header - and every header included by that - with the integer literal 4.

Every local variable that was called "size" is now just the character 4. Any method called size is now called 4. These text substitutions happen everywhere, even when they make no sense.

When macros are used, they're named something like MY_UNIQUE_PREFIX_MACRO specifically to avoid problems like this.

CodePudding user response:

I've looked at the errors compiling your code, and the problem seems to be at line 1: #define size 4, size seems to be a macro used in other source files in g . Renaming this to sz solves the problem. As well, the if statements use only 1 =, for these if statements, use == instead.

  •  Tags:  
  • c
  • Related