Rahul R Dhobi
Rahul R Dhobi

Reputation: 5816

compilation error while using swap method for queue in C++

I have error: ‘class std::queue<int>’ has no member named ‘swap’ while compiling following code

#include <iostream>       // std::cout
#include <queue>          // std::queue

int main ()
{
  std::queue<int> foo,bar;
  foo.push (10); foo.push(20); foo.push(30);
  bar.push (111); bar.push(222);

  foo.swap(bar);

  std::cout << "size of foo: " << foo.size() << '\n';
  std::cout << "size of bar: " << bar.size() << '\n';

  return 0;
}

I'm using g++ (Ubuntu/Linaro 4.6.3-1ubuntu5) 4.6.3 for compiling this code,can anyone have a idea for this error?

Upvotes: 1

Views: 2629

Answers (1)

marcinj
marcinj

Reputation: 50026

Use:

std::swap(foo, bar);

It appear that since c++11, you have std::queue::swap

http://www.cplusplus.com/reference/queue/queue/swap-free/

g++ 4.6 appears to not accept -std=c++11, so you must upgrade your compiler for this method to work.

[edit]

g++ 4.6 accepts -std=c++0x to enable c++11

Upvotes: 5

Related Questions