【问题标题】:Problems comparing value to member of a struct将值与结构成员进行比较的问题
【发布时间】:2013-05-29 10:28:12
【问题描述】:

此问题是对以下问题的跟进: Redefinition and Enumerator

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

1>forgelib\source\socket.cpp(145): warning C4832: token '.' is illegal after UDT 'Forge::AddressFamily'
1>          forgelib\include\forge\socket.h(70) : see declaration of 'Forge::AddressFamily'
1>forgelib\source\socket.cpp(145): error C2275: 'Forge::AddressFamily' : illegal use of this type as an expression
1>          forgelib\include\forge\socket.h(70) : see declaration of 'Forge::AddressFamily'
1>forgelib\source\socket.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;
};

我是否遗漏了一些非常明显的东西?

【问题讨论】:

    标签: c++ struct comparison constants


    【解决方案1】:

    应该是

    he->h_addrtype != AddressFamily::INet
    

    因为INet 是结构的静态成员。

    【讨论】:

    • 谢谢。嗯,问题解决了。抱歉@JuanchoPanza,但 ForEveR 领先于你,所以我不得不接受他的回答。
    • @JesseBrands 没问题,虽然我的时钟让我先回答 :-) 但我不在乎,答案都是一样的。
    【解决方案2】:

    INetAddressFamily 的静态成员,所以你需要

    AddressFamily::INet
    

    【讨论】:

      【解决方案3】:

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

      AddressFamily::INet
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2020-09-19
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2018-01-24
        • 1970-01-01
        • 2021-08-29
        相关资源
        最近更新 更多