0

So I'm trying to overload the extraction operator, but I keep getting an ambiguous overload error. Here's the extraction that I'm using:

bool cc = false;
cout << CC;

And here's the overload function.

ostream& operator<< (ostream &out, bool &C1){
    if(C1) out << "A";
    if(!C1) out << "D";
    return out;
}

Any ideas why I'm getting the error?

2
Contributors
3
Replies
4
Views
6 Years
Discussion Span
Last Post by caut_baia
0

You can't since it is already overloaded for simple types.Try using a function or explain your intent.

0

I want to be able to set the program so that every time I print out a bool value, it will display a character based on whether it's true or false. I could just just an if statement when I print it, but I thought maybe overloading << would be a bit easier.

0

It would actually be more difficult and redundant despite impossible.Try something this

std::string Bool (bool Cl)  {
   std::string Res[2]={"A","D"};
   return Res[Cl];
 }
This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.