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

时间:2013-09-28 08:33:53

标签: c++ linux struct g++

是否可以在命名空间内引用结构类型(例如在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’ 

由于某些原因,那些&#34;全球&#34; struct属于std赢得了上面的问题(我试图替换&#34; struct epoll_event&#34;用&#34; struct tm&#34;,它工作正常)。

所以2个问题:
1.参考那些&#34;全球&#34;的正确方法是什么?非std结构?
2.为什么要提到那些&#34;全球&#34;编译器对std结构进行了不同的处理(至少是g ++ 4.4.x)?

提前致谢。

1 个答案:

答案 0 :(得分:2)

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

更新

#include <sys/epoll>

#include <sys/epoll.h>