【问题标题】:error C2373: 'inet_addr' : redefinition; different type modifiers错误 C2373:“inet_addr”:重新定义;不同的类型修饰符
【发布时间】:2015-08-14 10:11:08
【问题描述】:

我正在编写其他人的代码,该代码是为某些套接字编程而编写的。该项目有以下两个文件。

SOCKUTIL.H

#if !defined(SOCKUTIL_H)
#define SOCKUTIL_H

unsigned long   inet_addr(const char *sIp);

unsigned short  htons(unsigned short port);
#endif

sockUtil.cpp

#include "stdafx.h"
#include "sockutil.h"
#include <stdlib.h>
#include <string.h>

unsigned long inet_addr(const char *sIp)
{
    int             octets[4];
    int             i;
    const char      *auxCad = sIp;
    unsigned long   lIp = 0;

    //we extract each octet of the ip address
    //atoi will get characters until it found a non numeric character(in our case '.')
    for(i = 0; i < 4; i++)
    {
        octets[i] = atoi(auxCad);

        if(octets[i] < 0 || octets[i] > 255)
        {
            return(0);
        }

        lIp |= (octets[i] << (i * 8));

        //update auxCad to point to the next octet
        auxCad = strchr(auxCad, '.');

        if(auxCad == NULL && i != 3)
        {
            return(0);
        }

        auxCad++;
    }

    return(lIp);
}

unsigned short htons(unsigned short port)
{
    unsigned short  portRet;

    portRet = ((port << 8) | (port >> 8));

    return(portRet);
}

这个项目最初是用VC6开发的,当我用VS2013打开它时,Visual Studio对其进行了转换。但是当我 build 它时,它会给出以下错误。

错误 C2373:“inet_addr”:重新定义;不同的类型修饰符

错误 C2373:'htons':重新定义;不同的类型修饰符

我试图找到解决方案,但不知道该怎么做。我对此了解不多。

编辑:此代码不使用 #include Winsock2.h。我检查了几个在线解决方案,声称这个库是重新定义的原因,但在这种情况下并非如此。

【问题讨论】:

    标签: c++ sockets visual-c++ visual-studio-2013


    【解决方案1】:

    在较新版本的 Visual Studio 中已经为您定义了这些函数(请参阅:MSDN) - 您可以从项目中删除这些文件并删除所有出现的:

    #include "sockutil.h"
    

    【讨论】:

      猜你喜欢
      • 2016-11-05
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-04-15
      • 2016-09-27
      相关资源
      最近更新 更多