0

I am running into a couple of errors related to "htonf" c++ function error while compiling my code. Help will be much appreciated.

Following are the errors:

Error C2556 'long htonf(float)': overloaded function differs only by return type from 'unsigned int htonf(float)'

Error C2371 'htonf': redefinition; different basic types ecueHost

Error C2065 'htonf': undeclared identifier

The error is appearing in the datapacket.cpp below

#include "str.h"
#include "DataPacket.h"
#include "exception.h"
#include "message.h"
#include "object.h"
#include "util.h"


#define MAX_DATA_LENGTH  4096

long htonf(float f)
{
    long x;
    x = *((long*)&f);
    x = htonl(x);
    return x;
}

float ntohf(long l)
{
    float f;
    l = ntohl(l);
    f = *((float*)&l);
    return f;
}

In the "winsock2.h" header file included in "datapacket.h" header, the "htonf" is defined as under:

#ifndef htonf
__inline unsigned __int32 htonf ( float Value ) 
{ 
unsigned __int32 Tempval;
unsigned __int32 Retval;
Tempval = *(unsigned __int32*)(&Value);
Retval = _WS2_32_WINSOCK_SWAP_LONG 
(Tempval);
return Retval;
}
#endif /* htonf */

and in the "datapacket.cpp" file itself the "htonf" is also declared here

// Store a float to the datapacket
TDataPacket& TDataPacket::operator<<(float f)
{
long x = htonf(f);
return SerializingIn(&x, LONG_SIZE);
}
wam
  • 1
  • 1
  • The clue is in the second error message `'htonf': redefinition;`. What that means is that one (or more) of those header files you've included is declaring `htonf` in a different way to what you are using in `datapacket.cpp`. So have a look through the header files for `htonf` and post anything you find here. – john Dec 22 '18 at 08:59
  • Could you please attempt to create a [mcve] to show us? When and where and *how* are you *declaring* those functions? – Some programmer dude Dec 22 '18 at 09:00
  • 1
    Incidentally that code is very dubious because it assumes that a `float` and a `long` are the same size, which isn't necessarilly true (especially in the age of 64 bit computing). Where did you get it from? – john Dec 22 '18 at 09:01
  • 1
    By the way, doing type-punning like that breaks [*strict aliasing*](https://stackoverflow.com/questions/98650/what-is-the-strict-aliasing-rule). You need to go through byte (`char`) arrays to be fully compliant. – Some programmer dude Dec 22 '18 at 09:06
  • @john this is a pretty old code which I am trying to recompile – wam Dec 22 '18 at 09:12
  • @john I have posted the declaration of htonf in header file. Can you suggest something now please? – wam Dec 22 '18 at 09:28
  • Just take advantage of the #ifndef in the winsock2 header file. `#define htonf htonf`gets the job done. You don't even have to change the code, set the compile option to define the symbol. – Hans Passant Dec 22 '18 at 10:30

1 Answers1

0
Error C2556 'long htonf(float)': overloaded function differs only by return type from 'unsigned int htonf(float)'

This error message explains it completely.

You have:

__inline unsigned __int32 htonf ( float Value ) 

in winsock2.h, and

long htonf(float f)

in datapacket.cpp. Your easiest solution will be to change the definition of htonf() in datapacket.cpp to match what you have in winsock2.h, and adjust the implementation as needed to reflect the new return type.

First of all, long is signed while unsigned __int32 is unsigned, and secondly, even though they are the same size under MSVC, __int32 and long are not interchangeable data types.

dgnuff
  • 3,195
  • 2
  • 18
  • 32