narecia
narecia

Reputation: 11

How to return bitset value in C++?

How do I return a bitset from a function in C++?

bitset<32>* check() {
  bitset<32> v8;
  for (int i = 0; i <= 5; i++) {
    v8[i] = 1;
  }
  return v8;
}

I got this error:

[Error] cannot convert 'std::bitset<32u>' to std::bitset<32u>*' in return

Upvotes: 0

Views: 1019

Answers (2)

Casper Beyer
Casper Beyer

Reputation: 2301

You are trying to return a value as a pointer, in this case you really should just return by value. Using a pointer is nonsense here.

bitset<32> check() {
  bitset<32> v8;
  for (int i = 0; i <= 5; i++) {
    v8[i] = 1;
  }

  return v8;
}

Upvotes: 2

eerorika
eerorika

Reputation: 238291

You cannot return a bitset from a function that is declared to return a pointer to a bitset.

You can return a bitset from a function that is declared to return a bitset:

bitset<32>  check()
//        ^ note the lack of * which would be syntax for a pointer declaration

Upvotes: 1

Related Questions