0

I have tried to write the following code into my compiler and compile it:

    #include <iostream>
#include <bitset>
using namespace std;

void binary(int a)
{
 cout << bitset<8>(a).to_string() << endl;
}

int main()
{

binary(16);

system("pause");
return 0;    
}

It should give me a binary output but I keep getting an error:

In function `void binary(int)': 
no matching function for call to `std::bitset<8u>::to_string()' 

I am new to C++ and dont really know what this means, please help me.

mamta rani
  • 133
  • 1
  • 2
  • 7

2 Answers2

2

I think older versions of bitset::to_string<T>() takes a template argument. So this should work:

cout << bitset<8>(a).to_string<char>() << endl;
suvayu
  • 4,271
  • 2
  • 29
  • 35
-1

bitset don't have a to_string method (stl does not use to_string anyway). You should iterate on values yourself.

Emanuele Paolini
  • 9,912
  • 3
  • 38
  • 64
  • Is this not a case of "what compiler/C++library, and what settings"? If the compiler supports C++11 (or sufficiently late C++0x), it should work fine, but with default settings, it wont? – Mats Petersson Jan 27 '13 at 13:16
  • 2
    http://en.cppreference.com/w/cpp/utility/bitset/to_string it does exist even before C++11, although with less defaults/flexibility. – Matteo Italia Jan 27 '13 at 13:18