Oveloaded function, ambigious call

hi,

I’m writing a small test serial library and getting the - call of overloaded ‘write(int)’ is ambiguous - error.

Here are the functions:

Header file declarations

 static void write(byte s);
 static void write(uint16_t ut)

CPP definitions

void mySerial_atmega8::write(byte s){
	while (! (UCSRA & (1<<UDRE)) ); //wait for empty transmit buffer
	UDR = s; //Put data into buffer, write.
}

void mySerial_atmega8::write(uint16_t ut){
	write((byte)(ut >> 8));
	write((byte)(ut));
}

If I declare a byte or an int variable it works fine, e.g.

byte var = 200;
mySerial_atmega8::write(var);

however

mySerial_atmega8::write(200);

produces the ambiguous error.

Is it because the compiler can’t tell whether 200 is a byte or an int?

What happens with

mySerial_atmega8::write((unsigned int)200);

I think it is because your prototype is looking for an unsigned int and the constant 200 is being treated as a regular signed int.

Delta_G: What happens with

mySerial_atmega8::write((unsigned int)200);

That does work, but I'd like to have this done automatically, like the Arudino Serial library does. I've been looking at the HardwareSerial.h and cpp files, but wasn't able to figure out how it does that.

That does work, but I'd like to have this done automatically, like the Arudino Serial library does. I've been looking at the HardwareSerial.h and cpp files, but wasn't able to figure out how it does that.

It has an overload for the common types. You still need to cast some constants, such as 0 which could mean a lot of things.

All of your overloads can call one method, where you do the casting, so the user doesn't have to.