C++ moving an element from Front of a queue to Back

here is my code for the function:
1
2
3
4
5
6
int move_to_rear(int Item)
{   
    linkedQueueType<int> myQueue;
const Item first = myQueue.front();   //getting the first
myQueue.deleteQueue();     //moving him to the back;
myQueue.addQueue(first);   //adding him back to the queue(which will be in the rear)  


Here is the program:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
#include<iostream>
#include "linkedQueue.h"
#include<queue>
using namespace std;

int move_to_rear(int Item);

 int main()
{
    int Item;
    linkedQueueType<int> myQueue; 
    myQueue.addQueue(1732);
    myQueue.addQueue(1767);
    myQueue.addQueue(1734);
    myQueue.addQueue(1764);
    myQueue.addQueue(1766);

    move_to_rear(1732);

After the execution of this program, 1732 sould be the last element
I get there two errors:
1
2
3
4
5
6
7
8
9
C:\Martin\MalikChapter8\Chapter 8 Source Code\linkedQueue\queueTest.cpp:30:8: error: 'Item' does not name a type
        ^
C:\Martin\MalikChapter8\Chapter 8 Source Code\linkedQueue\queueTest.cpp:32:18: error: 'first' was not declared in this scope
 myQueue.addQueue(first);   //adding him to the back to the queue(which will be the rear
                  ^
Process terminated with status 1 (0 minute(s), 0 second(s))
2 error(s), 0 warning(s) (0 minute(s), 0 second(s))
 
1
2
3
4
5
6
7
8
9
10
int move_to_rear(int Item)
{   
    linkedQueueType<int> myQueue;
const Item first = myQueue.front();   //getting the first
myQueue.deleteQueue();     //moving him to the back;
myQueue.addQueue(first);   //adding him back to the queue(which will be in the rear) 

///

move_to_rear(1732);

Why isn't move_to_rear a member function?
Why do you create a completely new temporary myQueue inside the function?
Item = myQueue.front();

Last edited on
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
#include <iostream>
#include <queue>
using namespace std; 

int main(){
	queue<int> a;	
	for(int i=1; i<=5; i++){
		a.push(i);
	} //here a={1,2,3,4,5}
	a.push(a.front()); // now a={1,2,3,4,5,1}
	a.pop(); // now a={2,3,4,5,1}
	while(!a.empty()){
		cout << a.front() << " ";
		a.pop();
	}		
return 0;
}
Last edited on
> Why isn't move_to_rear a member function?
¿why should it? it can be implemented fine as a non-friend free function.


int move_to_rear(int Item)
¿what's the purpose of that parameter?
¿and what do you return?

> error: 'Item' does not name a type
just a couple of lines above you said that `Item' was a variable, not a type
also, ¿is your function supposed to be a template?

myQueue.deleteQueue(); //moving him to the back;
that comment doesn't correspond with that line
Topic archived. No new replies allowed.