比较值与结构体成员的问题

Problems comparing value to member of a struct

本文关键字:问题 成员 结构体 比较      更新时间:2023-10-16

这个问题是关于重定义和枚举符

我的c++编译器给出以下警告和错误:

1>forgelibsourcesocket.cpp(145): warning C4832: token '.' is illegal after UDT 'Forge::AddressFamily'
1>          forgelibincludeforgesocket.h(70) : see declaration of 'Forge::AddressFamily'
1>forgelibsourcesocket.cpp(145): error C2275: 'Forge::AddressFamily' : illegal use of this type as an expression
1>          forgelibincludeforgesocket.h(70) : see declaration of 'Forge::AddressFamily'
1>forgelibsourcesocket.cpp(145): error C2228: left of '.INet' must have class/struct/union

这是socket.cpp(145):

void Forge::InternetHost::validHostEntity(hostent* he) {
    if (he->h_addrtype != AddressFamily.INet || he->h_length != 4) { // <--145
        throw new Forge::SocketException("Address family mismatch.");
    }
}

这是定义:

struct AddressFamily {
    static const Forge::Int Unspecified = AF_UNSPEC;
    static const Forge::Int UNIX = AF_UNIX;
    static const Forge::Int INet = AF_INET;
    static const Forge::Int IPX = AF_IPX;
    static const Forge::Int AppleTalk = AF_APPLETALK;
    static const Forge::Int INet6 = AF_INET6;
};

我是不是错过了一些很明显的东西?

应该是

he->h_addrtype != AddressFamily::INet
因为INet是struct的静态成员。

INetAddressFamily的静态成员,所以需要

AddressFamily::INet

您应该尝试这样访问静态成员:

AddressFamily::INet