The following two programs are identical to me but 1 shows correct results

here is the first one:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
#include <iostream>
#include <queue>
#include <list>
using namespace std;

int main() {
    queue<int> q1;
    queue<int> q2;
   int i = 0;
   while(i!= -999)
   {
       q1.push(i);
       cin >>i;
   }
       q2 = q1;

   cout << "Contents of q1: " ;
   while (!q1.empty()) {
      cout << q1.front() << "  ";
      q1.pop();
   }

   cout << endl;

   cout << "Contents of q2: " ;
   while (!q2.empty()) {
      cout << q2.front() << "  ";
      q2.pop();
   }

   return 0;
}

here is the correct output below:
1
2
3
4
5
6
23 45 67 89 -999
Contents of q1: 0  23  45  67  89
Contents of q2: 0  23  45  67  89
Process returned 0 (0x0)   execution time : 36.094 s
Press any key to continue.

Here is the second one which gives an error:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
// CPP code to illustrate
// Queue in Standard Template Library (STL)
#include <iostream>
#include <queue>
#include<list>

using namespace std;

void showq(queue <int> gq)
{
    queue <int> g = gq;
    while (!g.empty())
    {
        cout << '\t' << g.front();
        g.pop();
    }
    cout << '\n';
}

int main(void)
{
    queue <int> gquiz;
    queue <int> gquiz2;
    gquiz.push(10);
    gquiz.push(20);
    gquiz.push(30);

    gquiz2 = gquiz;
    showq(gqiuz2);
    cout << "The queue gquiz is : ";
    showq(gquiz);

    cout << "\ngquiz.size() : " << gquiz.size();
    cout << "\ngquiz.front() : " << gquiz.front();
    cout << "\ngquiz.back() : " << gquiz.back();

    cout << "\ngquiz.pop() : ";
    gquiz.pop();
    showq(gquiz);

    return 0;
}

Here is the error produced:
1
2
3
4
5
6
7
8
9
mingw32-g++.exe   -c C:\Martin\MalikChapter8\MArtinCopyQueue\MartinCopyQueue\geeksQueue.cpp -o C:\Martin\MalikChapter8\MArtinCopyQueue\MartinCopyQueue\geeksQueue.o
C:\Martin\MalikChapter8\MArtinCopyQueue\MartinCopyQueue\geeksQueue.cpp: In function 'int main()':
C:\Martin\MalikChapter8\MArtinCopyQueue\MartinCopyQueue\geeksQueue.cpp:29:11: error: 'gqiuz2' was not declared in this scope
     showq(gqiuz2);
           ^
Process terminated with status 1 (0 minute(s), 1 second(s))
1 error(s), 0 warning(s) (0 minute(s), 1 second(s))
 
  

I am not sure as to why the second one gives the error.
Check your spelling.
1
2
    gquiz2 = gquiz;  // QU
    showq(gqiuz2);  // QI 
Thank you salem c

here is the correct output now:
1
2
3
4
5
6
7
8
9
10
11
12
13
gquiz2 output is:       10      20      30
The queue gquiz is :    10      20      30

gquiz.size() : 3
gquiz.front() : 10
gquiz.back() : 30
gquiz.pop() :   20      30

Process returned 0 (0x0)   execution time : 2.338 s
Press any key to continue.

What is puzzling to me is where does the equality function coming from?
I have checked all the function under <queue.h> and the function for operator= is not here
http://www.cplusplus.com/reference/queue/queue/

queues are implemented as containers adaptors, which are classes that use an encapsulated object of a specific container class as its underlying container, providing a specific set of member functions to access its elements. Elements are pushed into the "back" of the specific container and popped from its "front".

and

The standard container classes deque and list fulfill these requirements. By default, if no container class is specified for a particular queue class instantiation, the standard container deque is used.

The underlying container provides the assignment operator, along with everything else which isn't just unique to queue.
Topic archived. No new replies allowed.