0

#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.

  • 1
    `using namespace std;` may be part of your problem. The standard library has a `size` and `queue`. Read about the problems it can cause here: [https://stackoverflow.com/questions/1452721/why-is-using-namespace-std-considered-bad-practice](https://stackoverflow.com/questions/1452721/why-is-using-namespace-std-considered-bad-practice) – drescherjm Oct 28 '22 at 21:21
  • 2
    `if (front = rear)` is a most likely a bug. `=` is an assignment. Use `==` for comparison. – drescherjm Oct 28 '22 at 21:21
  • 1
    `if (rear = size)` and `if (front = rear)` both look wrong. Renaming `size` to something like `sz` fixes the issue. https://godbolt.org/z/x1fKTvjf7 Gotta be careful with defines, the potential for a conflict is huge, especially with `using namesapce std`. – Retired Ninja Oct 28 '22 at 21:24

2 Answers2

5

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.

Useless
  • 64,155
  • 6
  • 88
  • 132
0

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.

xingharvey
  • 131
  • 6
  • 2
    It doesn't have to be a macro (and I'm sure the standard library is better written than to use a macro with a name like that). It just has to be a token. – Useless Oct 28 '22 at 21:40