Heoi, I've seen a C++ talk where someone made a lambda fizzbuzz implementation.
This is not it! Not even close to it! My question is, why can't I use the ostream&
auto fizz = [](int& x, std::ostream& os) { x % 3 == 0 ? os << "fizz" : 0; };
auto buzz = [](int& x, std::ostream& os) { x % 5 == 0 ? os << "buzz" : 0; };
for (int i = 0; i != 100; ++i)
{
fizz(i, std::cout);
buzz(i, std::cout);
}
And my Error message is :
E1776 function "std::basic_ostream<_Elem, _Traits>::basic_ostream(const std::basic_ostream<_Elem, _Traits>::_Myt &) [with _Elem=char, _Traits=std::char_traits<char>]" (declared at line 83 of "c:\Program Files (x86)\Microsoft Visual Studio\2017\Community\VC\Tools\MSVC\14.12.25827\include\ostream") cannot be referenced -- it is a deleted function 56
Your problem is quite clear. Since std::ostream and int are not of the same type, providing types that are not the same to the ternary operator creates an error. To solve this, you probably want to avoid an else clause altogether, so your functions would look like this:
auto fizz = [](int& x, std::ostream& os) { if (x % 3 == 0) os << "fizz"; };
auto buzz = [](int& x, std::ostream& os) { if (x % 5 == 0) os << "buzz"; };
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With