ntruchsess / arduino_uip

UIPEthernet: A plugin-replacement of the stock Arduino Ethernet library for ENC28J60 shields and breakout boards. Full support for persistent (streaming) TCP-connections and UDP (Client and Server each), ARP, ICMP, DHCP and DNS. Build around Adam Dunkels uIP Stack. Further developed version can be found on https://github.com/UIPEthernet/UIPEthernet
489 stars 211 forks source link

Ambiguous overload for 'operator==' #91

Closed markheloking closed 9 years ago

markheloking commented 9 years ago

The code I'm using is:

void EtherSetup()
{
  int DHCPOK = 0;
  int NoIP = 0;

  do
  {
    #if USEDHCP == 1
    Ethernet.begin(mac);
    #else
    Ethernet.begin(mac,myIP);
    #endif

    if(Ethernet.localIP() == NoIP)
    {
      Serial.println(F("Error fetching DHCP info\n"));
      DHCPOK = 0;
    }
    else
    {
      DHCPOK = 1;
    }
  }while(DHCPOK == 0);

  Serial.println(F("-=-=-=-=-=-=-=-=-=-=\n"));
  Serial.print(F("IP Address        : "));
  Serial.println(Ethernet.localIP());
  Serial.print(F("Subnet Mask       : "));
  Serial.println(Ethernet.subnetMask());
  Serial.print(F("Default Gateway IP: "));
  Serial.println(Ethernet.gatewayIP());
  Serial.print(F("DNS Server IP     : "));
  Serial.println(Ethernet.dnsServerIP());
}

Then the compiler gives me an error on if(Ethernet.localIP() == NoIP)

RFIDValidate_3_Buzzer.ino: In function 'void EtherSetup()':
RFIDValidate_3_Buzzer:129: error: ambiguous overload for 'operator==' in 'UIPEthernetClass::localIP()() == NoIP'
RFIDValidate_3_Buzzer.ino:129: note: candidates are: operator==(uint32_t, int) <built-in>
D:\Dropbox\Programmeren\RFID\Arduino\Arduino IDE 1.0.5-r2\hardware\arduino\cores\arduino/IPAddress.h:52: note:                 bool IPAddress::operator==(const IPAddress&)
D:\Dropbox\Programmeren\RFID\Arduino\Arduino IDE 1.0.5-r2\hardware\arduino\cores\arduino/IPAddress.h:53: note:                 bool IPAddress::operator==(const uint8_t*) <near match>

Seems like there was a problem with overloading the == operator, though I cannot seem to figure out how to resolve it (I cannot dictate it which one to use or something similar). What I want to achieve with this piece of code is that when the DHCP times out, it just gives 0.0.0.0 as IP, mask, etc. Since 0.0.0.0 is never a valid IP in my situation, I would like to check against it to see if the details were correctly fetched using DHCP. Am I doing something wrong?

ntruchsess commented 9 years ago

The compiler complains because NoIP is int and thus is not an exact match to the operator== defined in https://github.com/arduino/Arduino/blob/master/hardware/arduino/cores/arduino/IPAddress.h#L46 use IPAddress NoIP = IPAddress(0) should resolve the problem.

ntruchsess commented 9 years ago

closing as this is not caused by UIPEthernet but invalid use of == comparing to unsuitable type

markheloking commented 9 years ago

The code I'm using reflecting your changes:

void EtherSetup()
{
  int DHCPOK = 0;
  IPAddress NoIP = IPAddress(0);

  do
  {
    #if USEDHCP == 1
    Ethernet.begin(mac);
    #else
    Ethernet.begin(mac,myIP);
    #endif

    if(Ethernet.localIP() == NoIP)
    {
      Serial.println(F("Error fetching DHCP info\n"));
      DHCPOK = 0;
    }
    else
    {
      DHCPOK = 1;
    }
  }while(DHCPOK == 0);

  Serial.println(F("-=-=-=-=-=-=-=-=-=-=\n"));
  Serial.print(F("IP Address        : "));
  Serial.println(Ethernet.localIP());
  Serial.print(F("Subnet Mask       : "));
  Serial.println(Ethernet.subnetMask());
  Serial.print(F("Default Gateway IP: "));
  Serial.println(Ethernet.gatewayIP());
  Serial.print(F("DNS Server IP     : "));
  Serial.println(Ethernet.dnsServerIP());
}

Then the compiler gives me an error on IPAddress NoIP = IPAddress(0);

RFIDValidate_4_PostMethod.ino: In function 'void EtherSetup()':
RFIDValidate_4_PostMethod:121: error: call of overloaded 'IPAddress(int)' is ambiguous
D:\Dropbox\Programmeren\RFID\Arduino\Arduino IDE 1.0.5-r2\hardware\arduino\cores\arduino/IPAddress.h:47: note: candidates are: IPAddress::IPAddress(const uint8_t*)
D:\Dropbox\Programmeren\RFID\Arduino\Arduino IDE 1.0.5-r2\hardware\arduino\cores\arduino/IPAddress.h:46: note:                 IPAddress::IPAddress(uint32_t)
D:\Dropbox\Programmeren\RFID\Arduino\Arduino IDE 1.0.5-r2\hardware\arduino\cores\arduino/IPAddress.h:33: note:                 IPAddress::IPAddress(const IPAddress&)

I had to use IPAddress NoIP = IPAddress((uint32_t)0);

Is there nothing useful to be done about this? It's rather annoying to cast every integer I'm going to use as an argument...

ntruchsess commented 9 years ago

This is not an issue with UIPEthernet, IPAddress is part of core Arduino.

Good places to discuss and or address this issue are:

http://forum.arduino.cc/index.php?board=11.0 https://groups.google.com/a/arduino.cc/forum/#!forum/developers https://github.com/arduino/Arduino/issues

and before you start to complain there you should read http://www.learncpp.com/cpp-tutorial/76-function-overloading/