智能感知:错误C2440:'=':无法从'unsigned long(__stdcall *)(const char *)'转换为'IPAddr'

时间:2013-12-04 23:02:26

标签: c++ visual-studio intellisense ping icmp

我正在尝试编写一个ping IP地址的应用程序,并提供与ping相关的相关信息。虽然我收到了大量的错误,但这是我选择尝试并首先学会解决的错误!我有一小部分代码和一个包含编译器错误的单独文档。如果有人可以将此错误翻译成英语,那就太棒了。我觉得如果我把底部的两个错误得到照顾,很多其他错误可能会更容易确定。提前谢谢。

#include <iostream>
#include <stdio.h>
#include <Windows.h>
#include <iphlpapi.h>
#include <icmpapi.h>
#include <string>
#include <winsock2.h>
#pragma comment(lib, "iphlpapi.lib")
#pragma comment(lib, "ws2_32.lib")

using namespace std;

void CreateIcmpHandle(HANDLE &TheHdl){
    TheHdl = IcmpCreateFile();
}

 char* Getdestination(){
    char* theaddress;
    cout << "Where do you want to check? Enter the IP: ";
    cin >> theaddress;
    return theaddress;
}

void main(){
    HANDLE MyHandle;
    const char *theaddress;
    char Randomdata[30] = "Stuff to send away!";
    DWORD mainint,lenofbuffer;
    void* bufferpointer = NULL;
    ICMP_ECHO_REPLY* reply; //pointer to an ICMP_ECHO_REPLY data structure
    IPAddr address;
    lenofbuffer = strlen(Randomdata) + sizeof(ICMP4_ECHO_REPLY) + 1; //Make buffer large enough to contain data sent and the size of the ICMP_ECHO_REPLY data structure
    bufferpointer = malloc(lenofbuffer); //allocates a block of memory with number of bits of lenofbuffer
    memset(bufferpointer,0,lenofbuffer); //puts the previously allocated block in memory and writes lenofbuffer # of 0's
    reply = (ICMP_ECHO_REPLY*)bufferpointer;
    CreateIcmpHandle(MyHandle);
    theaddress = Getdestination();
    address = inet_addr(theaddress);
    mainint = IcmpSendEcho(MyHandle, address, Randomdata, strlen(Randomdata), NULL, bufferpointer, lenofbuffer, 2000); //Returns the number of replies from theaddress
    if(mainint = IP_SUCCESS){ //If the status of the ICMP_ECHO_REPLY structures stored in bufferpointer is IP_SUCCESS
        cout<<"Success! The ping returned "<< mainint <<" message replies from "<<reply->Address<<endl;
        cout<<"It took "<<reply->RoundTripTime<<" milliseconds."<<endl;
    }
    IcmpCloseHandle(MyHandle);
}

编译器错误:

c:\program files (x86)\windows kits\8.0\include\shared\ws2def.h(96): warning C4005: 'AF_IPX' : macro redefinition
1>          c:\program files (x86)\windows kits\8.0\include\um\winsock.h(452) : see previous definition of 'AF_IPX'
1>c:\program files (x86)\windows kits\8.0\include\shared\ws2def.h(136): warning C4005: 'AF_MAX' : macro redefinition
1>          c:\program files (x86)\windows kits\8.0\include\um\winsock.h(471) : see previous definition of 'AF_MAX'
1>c:\program files (x86)\windows kits\8.0\include\shared\ws2def.h(173): warning C4005: 'SO_DONTLINGER' : macro redefinition
1>          c:\program files (x86)\windows kits\8.0\include\um\winsock.h(394) : see previous definition of 'SO_DONTLINGER'
1>c:\program files (x86)\windows kits\8.0\include\shared\ws2def.h(217): error C2011: 'sockaddr' : 'struct' type redefinition
1>          c:\program files (x86)\windows kits\8.0\include\um\winsock.h(477) : see declaration of 'sockaddr'
1>c:\program files (x86)\windows kits\8.0\include\shared\ws2def.h(414): error C2059: syntax error : 'constant'
1>c:\program files (x86)\windows kits\8.0\include\shared\ws2def.h(414): error C3805: 'constant': unexpected token, expected either '}' or a ','
1>c:\program files (x86)\windows kits\8.0\include\shared\ws2def.h(549): warning C4005: 'IN_CLASSA' : macro redefinition
1>          c:\program files (x86)\windows kits\8.0\include\um\winsock.h(279) : see previous definition of 'IN_CLASSA'
1>c:\program files (x86)\windows kits\8.0\include\shared\ws2def.h(555): warning C4005: 'IN_CLASSB' : macro redefinition
1>          c:\program files (x86)\windows kits\8.0\include\um\winsock.h(285) : see previous definition of 'IN_CLASSB'
1>c:\program files (x86)\windows kits\8.0\include\shared\ws2def.h(561): warning C4005: 'IN_CLASSC' : macro redefinition
1>          c:\program files (x86)\windows kits\8.0\include\um\winsock.h(291) : see previous definition of 'IN_CLASSC'
1>c:\program files (x86)\windows kits\8.0\include\shared\ws2def.h(572): warning C4005: 'INADDR_ANY' : macro redefinition
1>          c:\program files (x86)\windows kits\8.0\include\um\winsock.h(296) : see previous definition of 'INADDR_ANY'
1>c:\program files (x86)\windows kits\8.0\include\shared\ws2def.h(574): warning C4005: 'INADDR_BROADCAST' : macro redefinition
1>          c:\program files (x86)\windows kits\8.0\include\um\winsock.h(298) : see previous definition of 'INADDR_BROADCAST'
1>c:\program files (x86)\windows kits\8.0\include\shared\ws2def.h(608): error C2011: 'sockaddr_in' : 'struct' type redefinition
1>          c:\program files (x86)\windows kits\8.0\include\um\winsock.h(304) : see declaration of 'sockaddr_in'
1>c:\program files (x86)\windows kits\8.0\include\um\winsock2.h(125): error C2011: 'fd_set' : 'struct' type redefinition
1>          c:\program files (x86)\windows kits\8.0\include\um\winsock.h(60) : see declaration of 'fd_set'
1>c:\program files (x86)\windows kits\8.0\include\um\winsock2.h(145): warning C4005: 'FD_CLR' : macro redefinition
1>          c:\program files (x86)\windows kits\8.0\include\um\winsock.h(89) : see previous definition of 'FD_CLR'
1>c:\program files (x86)\windows kits\8.0\include\um\winsock2.h(160): warning C4005: 'FD_SET' : macro redefinition
1>          c:\program files (x86)\windows kits\8.0\include\um\winsock.h(94) : see previous definition of 'FD_SET'
1>c:\program files (x86)\windows kits\8.0\include\um\winsock2.h(169): error C2011: 'timeval' : 'struct' type redefinition
1>          c:\program files (x86)\windows kits\8.0\include\um\winsock.h(103) : see declaration of 'timeval'
1>c:\program files (x86)\windows kits\8.0\include\um\winsock2.h(225): error C2011: 'hostent' : 'struct' type redefinition
1>          c:\program files (x86)\windows kits\8.0\include\um\winsock.h(159) : see declaration of 'hostent'
1>c:\program files (x86)\windows kits\8.0\include\um\winsock2.h(238): error C2011: 'netent' : 'struct' type redefinition
1>          c:\program files (x86)\windows kits\8.0\include\um\winsock.h(172) : see declaration of 'netent'
1>c:\program files (x86)\windows kits\8.0\include\um\winsock2.h(245): error C2011: 'servent' : 'struct' type redefinition
1>          c:\program files (x86)\windows kits\8.0\include\um\winsock.h(179) : see declaration of 'servent'
1>c:\program files (x86)\windows kits\8.0\include\um\winsock2.h(257): error C2011: 'protoent' : 'struct' type redefinition
1>          c:\program files (x86)\windows kits\8.0\include\um\winsock.h(191) : see declaration of 'protoent'
1>c:\program files (x86)\windows kits\8.0\include\um\winsock2.h(353): error C2011: 'WSAData' : 'struct' type redefinition
1>          c:\program files (x86)\windows kits\8.0\include\um\winsock.h(314) : see declaration of 'WSAData'
1>c:\program files (x86)\windows kits\8.0\include\um\winsock2.h(451): error C2011: 'sockproto' : 'struct' type redefinition
1>          c:\program files (x86)\windows kits\8.0\include\um\winsock.h(486) : see declaration of 'sockproto'
1>c:\program files (x86)\windows kits\8.0\include\um\winsock2.h(493): error C2011: 'linger' : 'struct' type redefinition
1>          c:\program files (x86)\windows kits\8.0\include\um\winsock.h(523) : see declaration of 'linger'
1>c:\program files (x86)\windows kits\8.0\include\um\winsock2.h(506): warning C4005: 'SOMAXCONN' : macro redefinition
1>          c:\program files (x86)\windows kits\8.0\include\um\winsock.h(536) : see previous definition of 'SOMAXCONN'
1>c:\program files (x86)\windows kits\8.0\include\um\winsock2.h(536): warning C4005: 'FD_READ' : macro redefinition
1>          c:\program files (x86)\windows kits\8.0\include\um\winsock.h(554) : see previous definition of 'FD_READ'
1>c:\program files (x86)\windows kits\8.0\include\um\winsock2.h(539): warning C4005: 'FD_WRITE' : macro redefinition
1>          c:\program files (x86)\windows kits\8.0\include\um\winsock.h(555) : see previous definition of 'FD_WRITE'
1>c:\program files (x86)\windows kits\8.0\include\um\winsock2.h(542): warning C4005: 'FD_OOB' : macro redefinition
1>          c:\program files (x86)\windows kits\8.0\include\um\winsock.h(556) : see previous definition of 'FD_OOB'
1>c:\program files (x86)\windows kits\8.0\include\um\winsock2.h(545): warning C4005: 'FD_ACCEPT' : macro redefinition
1>          c:\program files (x86)\windows kits\8.0\include\um\winsock.h(557) : see previous definition of 'FD_ACCEPT'
1>c:\program files (x86)\windows kits\8.0\include\um\winsock2.h(548): warning C4005: 'FD_CONNECT' : macro redefinition
1>          c:\program files (x86)\windows kits\8.0\include\um\winsock.h(558) : see previous definition of 'FD_CONNECT'
1>c:\program files (x86)\windows kits\8.0\include\um\winsock2.h(551): warning C4005: 'FD_CLOSE' : macro redefinition
1>          c:\program files (x86)\windows kits\8.0\include\um\winsock.h(559) : see previous definition of 'FD_CLOSE'
1>c:\program files (x86)\windows kits\8.0\include\um\winsock2.h(1577): error C2375: 'accept' : redefinition; different linkage
1>          c:\program files (x86)\windows kits\8.0\include\um\winsock.h(734) : see declaration of 'accept'
1>c:\program files (x86)\windows kits\8.0\include\um\winsock2.h(1599): error C2375: 'bind' : redefinition; different linkage
1>          c:\program files (x86)\windows kits\8.0\include\um\winsock.h(739) : see declaration of 'bind'
1>c:\program files (x86)\windows kits\8.0\include\um\winsock2.h(1618): error C2375: 'closesocket' : redefinition; different linkage
1>          c:\program files (x86)\windows kits\8.0\include\um\winsock.h(744) : see declaration of 'closesocket'
1>c:\program files (x86)\windows kits\8.0\include\um\winsock2.h(1637): error C2375: 'connect' : redefinition; different linkage
1>          c:\program files (x86)\windows kits\8.0\include\um\winsock.h(746) : see declaration of 'connect'
1>c:\program files (x86)\windows kits\8.0\include\um\winsock2.h(1658): error C2375: 'ioctlsocket' : redefinition; different linkage
1>          c:\program files (x86)\windows kits\8.0\include\um\winsock.h(751) : see declaration of 'ioctlsocket'
1>c:\program files (x86)\windows kits\8.0\include\um\winsock2.h(1679): error C2375: 'getpeername' : redefinition; different linkage
1>          c:\program files (x86)\windows kits\8.0\include\um\winsock.h(756) : see declaration of 'getpeername'
1>c:\program files (x86)\windows kits\8.0\include\um\winsock2.h(1700): error C2375: 'getsockname' : redefinition; different linkage
1>          c:\program files (x86)\windows kits\8.0\include\um\winsock.h(761) : see declaration of 'getsockname'
1>c:\program files (x86)\windows kits\8.0\include\um\winsock2.h(1723): error C2375: 'getsockopt' : redefinition; different linkage
1>          c:\program files (x86)\windows kits\8.0\include\um\winsock.h(766) : see declaration of 'getsockopt'
1>c:\program files (x86)\windows kits\8.0\include\um\winsock2.h(1744): error C2375: 'htonl' : redefinition; different linkage
1>          c:\program files (x86)\windows kits\8.0\include\um\winsock.h(773) : see declaration of 'htonl'
1>c:\program files (x86)\windows kits\8.0\include\um\winsock2.h(1761): error C2375: 'htons' : redefinition; different linkage
1>          c:\program files (x86)\windows kits\8.0\include\um\winsock.h(775) : see declaration of 'htons'
1>c:\program files (x86)\windows kits\8.0\include\um\winsock2.h(1778): error C2375: 'inet_addr' : redefinition; different linkage
1>          c:\program files (x86)\windows kits\8.0\include\um\winsock.h(777) : see declaration of 'inet_addr'
1>c:\program files (x86)\windows kits\8.0\include\um\winsock2.h(1795): error C2375: 'inet_ntoa' : redefinition; different linkage
1>          c:\program files (x86)\windows kits\8.0\include\um\winsock.h(779) : see declaration of 'inet_ntoa'
1>c:\program files (x86)\windows kits\8.0\include\um\winsock2.h(1896): error C2375: 'listen' : redefinition; different linkage
1>          c:\program files (x86)\windows kits\8.0\include\um\winsock.h(781) : see declaration of 'listen'
1>c:\program files (x86)\windows kits\8.0\include\um\winsock2.h(1914): error C2375: 'ntohl' : redefinition; different linkage
1>          c:\program files (x86)\windows kits\8.0\include\um\winsock.h(785) : see declaration of 'ntohl'
1>c:\program files (x86)\windows kits\8.0\include\um\winsock2.h(1931): error C2375: 'ntohs' : redefinition; different linkage
1>          c:\program files (x86)\windows kits\8.0\include\um\winsock.h(787) : see declaration of 'ntohs'
1>c:\program files (x86)\windows kits\8.0\include\um\winsock2.h(1951): error C2375: 'recv' : redefinition; different linkage
1>          c:\program files (x86)\windows kits\8.0\include\um\winsock.h(789) : see declaration of 'recv'
1>c:\program files (x86)\windows kits\8.0\include\um\winsock2.h(1976): error C2375: 'recvfrom' : redefinition; different linkage
1>          c:\program files (x86)\windows kits\8.0\include\um\winsock.h(795) : see declaration of 'recvfrom'
1>c:\program files (x86)\windows kits\8.0\include\um\winsock2.h(2002): error C2375: 'select' : redefinition; different linkage
1>          c:\program files (x86)\windows kits\8.0\include\um\winsock.h(803) : see declaration of 'select'
1>c:\program files (x86)\windows kits\8.0\include\um\winsock2.h(2026): error C2375: 'send' : redefinition; different linkage
1>          c:\program files (x86)\windows kits\8.0\include\um\winsock.h(810) : see declaration of 'send'
1>c:\program files (x86)\windows kits\8.0\include\um\winsock2.h(2051): error C2375: 'sendto' : redefinition; different linkage
1>          c:\program files (x86)\windows kits\8.0\include\um\winsock.h(816) : see declaration of 'sendto'
1>c:\program files (x86)\windows kits\8.0\include\um\winsock2.h(2077): error C2375: 'setsockopt' : redefinition; different linkage
1>          c:\program files (x86)\windows kits\8.0\include\um\winsock.h(824) : see declaration of 'setsockopt'
1>c:\program files (x86)\windows kits\8.0\include\um\winsock2.h(2099): error C2375: 'shutdown' : redefinition; different linkage
1>          c:\program files (x86)\windows kits\8.0\include\um\winsock.h(831) : see declaration of 'shutdown'
1>c:\program files (x86)\windows kits\8.0\include\um\winsock2.h(2120): error C2375: 'socket' : redefinition; different linkage
1>          c:\program files (x86)\windows kits\8.0\include\um\winsock.h(835) : see declaration of 'socket'
1>c:\program files (x86)\windows kits\8.0\include\um\winsock2.h(2144): error C2375: 'gethostbyaddr' : redefinition; different linkage
1>          c:\program files (x86)\windows kits\8.0\include\um\winsock.h(842) : see declaration of 'gethostbyaddr'
1>c:\program files (x86)\windows kits\8.0\include\um\winsock2.h(2163): error C2375: 'gethostbyname' : redefinition; different linkage
1>          c:\program files (x86)\windows kits\8.0\include\um\winsock.h(847) : see declaration of 'gethostbyname'
1>c:\program files (x86)\windows kits\8.0\include\um\winsock2.h(2181): error C2375: 'gethostname' : redefinition; different linkage
1>          c:\program files (x86)\windows kits\8.0\include\um\winsock.h(849) : see declaration of 'gethostname'
1>c:\program files (x86)\windows kits\8.0\include\um\winsock2.h(2221): error C2375: 'getservbyport' : redefinition; different linkage
1>          c:\program files (x86)\windows kits\8.0\include\um\winsock.h(853) : see declaration of 'getservbyport'
1>c:\program files (x86)\windows kits\8.0\include\um\winsock2.h(2240): error C2375: 'getservbyname' : redefinition; different linkage
1>          c:\program files (x86)\windows kits\8.0\include\um\winsock.h(857) : see declaration of 'getservbyname'
1>c:\program files (x86)\windows kits\8.0\include\um\winsock2.h(2258): error C2375: 'getprotobynumber' : redefinition; different linkage
1>          c:\program files (x86)\windows kits\8.0\include\um\winsock.h(861) : see declaration of 'getprotobynumber'
1>c:\program files (x86)\windows kits\8.0\include\um\winsock2.h(2275): error C2375: 'getprotobyname' : redefinition; different linkage
1>          c:\program files (x86)\windows kits\8.0\include\um\winsock.h(863) : see declaration of 'getprotobyname'
1>c:\program files (x86)\windows kits\8.0\include\um\winsock2.h(2296): error C2375: 'WSAStartup' : redefinition; different linkage
1>          c:\program files (x86)\windows kits\8.0\include\um\winsock.h(867) : see declaration of 'WSAStartup'
1>c:\program files (x86)\windows kits\8.0\include\um\winsock2.h(2315): error C2375: 'WSACleanup' : redefinition; different linkage
1>          c:\program files (x86)\windows kits\8.0\include\um\winsock.h(871) : see declaration of 'WSACleanup'
1>c:\program files (x86)\windows kits\8.0\include\um\winsock2.h(2332): error C2375: 'WSASetLastError' : redefinition; different linkage
1>          c:\program files (x86)\windows kits\8.0\include\um\winsock.h(873) : see declaration of 'WSASetLastError'
1>c:\program files (x86)\windows kits\8.0\include\um\winsock2.h(2349): error C2375: 'WSAGetLastError' : redefinition; different linkage
1>          c:\program files (x86)\windows kits\8.0\include\um\winsock.h(875) : see declaration of 'WSAGetLastError'
1>c:\program files (x86)\windows kits\8.0\include\um\winsock2.h(2366): error C2375: 'WSAIsBlocking' : redefinition; different linkage
1>          c:\program files (x86)\windows kits\8.0\include\um\winsock.h(877) : see declaration of 'WSAIsBlocking'
1>c:\program files (x86)\windows kits\8.0\include\um\winsock2.h(2383): error C2375: 'WSAUnhookBlockingHook' : redefinition; different linkage
1>          c:\program files (x86)\windows kits\8.0\include\um\winsock.h(879) : see declaration of 'WSAUnhookBlockingHook'
1>c:\program files (x86)\windows kits\8.0\include\um\winsock2.h(2400): error C2375: 'WSASetBlockingHook' : redefinition; different linkage
1>          c:\program files (x86)\windows kits\8.0\include\um\winsock.h(881) : see declaration of 'WSASetBlockingHook'
1>c:\program files (x86)\windows kits\8.0\include\um\winsock2.h(2417): error C2375: 'WSACancelBlockingCall' : redefinition; different linkage
1>          c:\program files (x86)\windows kits\8.0\include\um\winsock.h(883) : see declaration of 'WSACancelBlockingCall'
1>c:\program files (x86)\windows kits\8.0\include\um\winsock2.h(2439): error C2375: 'WSAAsyncGetServByName' : redefinition; different linkage
1>          c:\program files (x86)\windows kits\8.0\include\um\winsock.h(885) : see declaration of 'WSAAsyncGetServByName'
1>c:\program files (x86)\windows kits\8.0\include\um\winsock2.h(2466): error C2375: 'WSAAsyncGetServByPort' : redefinition; different linkage
1>          c:\program files (x86)\windows kits\8.0\include\um\winsock.h(893) : see declaration of 'WSAAsyncGetServByPort'
1>c:\program files (x86)\windows kits\8.0\include\um\winsock2.h(2492): error C2375: 'WSAAsyncGetProtoByName' : redefinition; different linkage
1>          c:\program files (x86)\windows kits\8.0\include\um\winsock.h(901) : see declaration of 'WSAAsyncGetProtoByName'
1>c:\program files (x86)\windows kits\8.0\include\um\winsock2.h(2517): error C2375: 'WSAAsyncGetProtoByNumber' : redefinition; different linkage
1>          c:\program files (x86)\windows kits\8.0\include\um\winsock.h(908) : see declaration of 'WSAAsyncGetProtoByNumber'
1>c:\program files (x86)\windows kits\8.0\include\um\winsock2.h(2542): error C2375: 'WSAAsyncGetHostByName' : redefinition; different linkage
1>          c:\program files (x86)\windows kits\8.0\include\um\winsock.h(915) : see declaration of 'WSAAsyncGetHostByName'
1>c:\program files (x86)\windows kits\8.0\include\um\winsock2.h(2569): error C2375: 'WSAAsyncGetHostByAddr' : redefinition; different linkage
1>          c:\program files (x86)\windows kits\8.0\include\um\winsock.h(922) : see declaration of 'WSAAsyncGetHostByAddr'
1>c:\program files (x86)\windows kits\8.0\include\um\winsock2.h(2592): error C2375: 'WSACancelAsyncRequest' : redefinition; different linkage
1>          c:\program files (x86)\windows kits\8.0\include\um\winsock.h(931) : see declaration of 'WSACancelAsyncRequest'
1>c:\program files (x86)\windows kits\8.0\include\um\winsock2.h(2612): error C2375: 'WSAAsyncSelect' : redefinition; different linkage
1>          c:\program files (x86)\windows kits\8.0\include\um\winsock.h(933) : see declaration of 'WSAAsyncSelect'
1>c:\users\craig\google drive\school\2013\fall 2013\computer networks\pingproject.cpp(38): error C2440: '=' : cannot convert from 'unsigned long (__stdcall *)(const char *)' to 'IPAddr'
1>c:\users\craig\google drive\school\2013\fall 2013\computer networks\pingproject.cpp(38): error C3861: 'inet_addr': identifier not found
1>
1>Build FAILED.

1 个答案:

答案 0 :(得分:2)

这是你的问题:

#include <Windows.h>
#include <iphlpapi.h>
#include <icmpapi.h>
#include <string>
#include <winsock2.h>

首先必须包含winsock2.h,否则windows.h将包含winsock.h。

尝试

#include <winsock2.h>
#include <Windows.h>
#include <iphlpapi.h>
#include <icmpapi.h>
#include <string>