引用命名空间中的结构类型

Referring to a struct type inside a namespace

本文关键字:结构 类型 命名空间 引用      更新时间:2023-10-16

是否可以在命名空间中引用结构类型(例如在sys/epoll.h中声明epoll_event)?

我试过:

#include <iostream>
#include <sys/epoll>
namespace N1 {
namespace N2 {
class C {
    public:
    void print() const {
        std::cout << sizeof(struct epoll_event) << std::endl;
     }
};
}
}
int main(int argc, char** argv) {
    N1::N2::C c;
    c.print();
    return 0;
}

g++给了我这个错误:

nested.cpp:3:21: error: sys/epoll: No such file or directory
nested.cpp: In member function ‘void N1::N2::C::print() const’:
nested.cpp:23: error: invalid application of ‘sizeof’ to incomplete type ‘N1::N2::epoll_event’ 

由于某些原因,那些属于 std 的"全局"结构不会出现上述问题(我尝试将"struct epoll_event"替换为"struct tm",并且工作正常)。

所以2个问题:
1. 引用那些"全局"非 std 结构的正确方法是什么?
2. 为什么编译器(至少 g++ 4.4.x)对那些"全局"std 结构的处理方式不同?

提前谢谢。

你只需要包含正确的头文件,否则你的代码是正确的。sys中有epoll.h,但没有epoll标题

更新

#include <sys/epoll>

#include <sys/epoll.h>