gpt4 book ai didi

c++ - 外部 "C"内部命名空间

转载 作者:行者123 更新时间:2023-11-30 01:28:00 25 4
gpt4 key购买 nike

你知道为什么下面的代码无法通过编译吗?

#include <iostream>

namespace C {
extern "C" {
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h> // open()
#include <unistd.h> // read()
}
}

int main(int argc, char** argv) {
int fd = C::open("./main.cpp", O_RDONLY);
C::read(fd, 0, 0);
return 0;
}

GCC 4.4 编译器的错误是:

error: ‘read’ is not a member of ‘C’

最佳答案

您不能将所有内容都注入(inject) header 下的命名空间中。在这种情况下,read 是一个宏,它在 namespace 解析规则生效之前被评估为其他内容。

关于c++ - 外部 "C"内部命名空间,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8089248/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com