C++ sendto() 给出"invalid conversion from ‘int (*)(int, int, int)throw ()’ to ‘int’"错误

C++ sendto() gives "invalid conversion from ‘int (*)(int, int, int)throw ()’ to ‘int’" error

本文关键字:int 错误 to throw from sendto 给出 invalid conversion C++      更新时间:2023-10-16

在过去的几周里,我一直在试图解决这个奇怪的问题,但没有成功。

基本上,我

正在尝试通过UDP向端点发送消息,并且我正在使用sys/socket.h

下面是给我错误的代码。

sendto(socket, msg.c_str(), msg.size(), 0, (struct sockaddr*) &endpoint, 
       sizeof(endpoint));

其中endpointsockaddr_in结构。

编译器抱怨:

错误:从"int (*)(int, int, int)throw ()"到 'int' [-fallowive]

这很有趣,因为该文件是一个组项目,并且代码的其他部分使用与上面完全相同的代码,并且编译器对这些代码感到满意。仅当我将该行复制粘贴到我在同一文件中写入的函数时,它才会给我此错误。

我想不出为什么会发生这种情况。任何想法将不胜感激。

此外,我正在编译-std=c++0x -Wall -pedantic

有一个具有三个int参数的socket()函数,
编译器认为您想将其作为第一个参数传递给sendto

显然,您在某处也有一个变量socket,"隐藏"套接字函数,但不在此范围内,因此它采用该功能。停止调用变量socket,这样会更容易找到(并且在变量已知的部分中调用socket函数也会更少问题)...

编译器

认为您正在将第一个参数中指向socket函数的指针传递给sendto,请尝试更改变量名称。