在编译程序时遇到 C++ Visual Studio 编译器错误"htonf"
Running into c++ visual studio compiler error "htonf" while compiling the program
我在编译代码时遇到了几个与"htonf"c++函数错误相关的错误。帮助将不胜感激。
以下是错误:
错误 C2556 "long htonf(float)":重载函数仅返回类型与"无符号 int htonf(float)"不同
错误 C2371 'htonf':重新定义;不同的基本类型 ecueHost
错误 C2065 "htonf":未声明的标识符
错误出现在数据包中.cpp如下所示
#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;
}
在"datapacket.h"头文件中包含的"winsock2.h"头文件中,"htonf"的定义如下:
#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 */
在"Datapacket.cpp"文件本身中,"htonf"也在这里声明
// Store a float to the datapacket
TDataPacket& TDataPacket::operator<<(float f)
{
long x = htonf(f);
return SerializingIn(&x, LONG_SIZE);
}
Error C2556 'long htonf(float)': overloaded function differs only by return type from 'unsigned int htonf(float)'
此错误消息完全解释了它。
你有:
__inline unsigned __int32 htonf ( float Value )
在 Winsock2.h 中,以及
long htonf(float f)
在数据包中.cpp。 最简单的解决方案是更改数据包中htonf()
的定义.cpp以匹配 winsock2.h 中的定义,并根据需要调整实现以反映新的返回类型。
首先,long
是有符号的,而unsigned __int32
是无符号的,其次,即使它们在 MSVC 下大小相同,__int32
和 long
也不是可互换的数据类型。
相关文章:
- MSVC多行宏编译器错误
- 静态数据成员的问题-修复链接错误会导致编译器错误
- C++,我收到一个无法理解的编译器错误
- 我收到同义重复编译器错误。我应该如何修复"类型"X"的参数与类型"X"的参数不兼容?
- 重载方法的方式会在使用临时调用时生成编译器错误
- 尝试使用继承和模板实现CRTP.Visual Studio正在生成编译器错误
- 编译器错误:destuctor 的更宽松的抛出说明符
- Android NDK clang 编译器错误在 Windows 上显示'No such file or directory'
- C++ G++ 编译器 - 错误:隐式声明的定义
- 这个失败的测试是将零添加到空指针未定义的行为、编译器错误还是其他什么?
- 模板模板参数和模板别名:编译器错误?
- C++ 编译器错误:P1LinkedList.cpp:145:错误:重载的"to_string(int&)"调用不明确
- 在gcc中意外调用了Const重载.编译器错误或兼容性修复程序
- C1001内部编译器错误是由于矢量初始化(如数组)引起的
- 编译器错误(英特尔并行工作室 2019 与 Visual Studio 社区 2019)
- 如何在C++中克服此 C4430 编译器错误?
- 尝试使用 WinBGI 显示文本时出现编译器错误
- 使用typedef'ed返回类型声明友元函数时出现编译器错误
- 如何在类模板的成员函数中正确调用函数对象?正在生成 Visual Studio 编译器错误 C2440
- C++自定义分配器大小参数作为模板参数会引发编译器错误