Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Use of overloaded operator ambiguous

The following code:

typedef void HELPER;

const HELPER* helper = _helper;

inline ostream& operator <<(ostream& out,  const HELPER* arg) 
{ out << (const char*)(arg); return out; }

Blows up if I attempt a

cout << helper;

Specifically, I get:

main.cpp:35:28: error: use of overloaded operator '<<' is ambiguous (with operand types 'basic_ostream >' and 'const HELPER *' (aka 'const void *'))

and it lists a few candidates:

/Library/Developer/CommandLineTools/usr/bin/../include/c++/v1/ostream:207:0: note: candidate function
    basic_ostream& operator<<(const void* __p);
                   ^
main.cpp:25:17: note: candidate function
inline ostream& operator <<(ostream& out,  const HELPER* arg) 
                ^
/Library/Developer/CommandLineTools/usr/bin/../include/c++/v1/ostream:195:20: note: candidate function
    basic_ostream& operator<<(bool __n);
                   ^

I'm a little surprised that my typedef isn't invoking a stronger type matching here. How can I get this operator overload running?

EDIT: Further clarification, the purpose of this code is that I am dual-targeting a set of Arduino libraries. They manage their strings frequently with:

typedef void __FlashStringHelper;

void showHelp(const __FlashStringHelper* helpText)
{
   Serial.print(helpText);
}

I like iostream and planned on this dual target, so I overloaded << on Serial object and made the previous into (this is the oversimplified version, for example)

#define cout Serial

void showHelp(const __FlashStringHelper* helpText)
{
   cout << helpText;
}

Now I want to actually target real iostream for a different arch, but the old Arduino code can't vary (much) from its __FlashStringHelpers. That's where I'm at

like image 758
Malachi Avatar asked Oct 15 '25 23:10

Malachi


1 Answers

typedef doesn't create types it aliases them,

inline ostream& operator <<(ostream& out,  const HELPER* arg) 

is equivalent to

inline ostream& operator <<(ostream& out,  const void* arg)

Maybe you wanted to create a type named HELPER

class HELPER{};
like image 53
Owen Delahoy Avatar answered Oct 17 '25 12:10

Owen Delahoy



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!