gpt4 book ai didi

c++ - using-declaration 不能命名命名空间

转载 作者:行者123 更新时间:2023-12-03 07:07:10 25 4
gpt4 key购买 nike

如果我有这样的文件,一切都会按预期工作:

#include <filesystem>
#include <iostream>
int main() {
std::filesystem::path o = "C:\\Windows\\write.exe";
auto s = o.parent_path();
std::cout << s << std::endl;
}

不过,如果可能的话,我想使用这样的一行:

filesystem::path o = "C:\\Windows\\write.exe";

我试过了,但出现错误:

// using-declaration may not name namespace 'std::filesystem'
using std::filesystem;

还有这个错误:

using namespace std::filesystem;
// error: 'filesystem' has not been declared
filesystem::path o = "C:\\Windows\\write.exe";

是否有可能做我正在尝试的事情?

最佳答案

你可以使用像这样的命名空间别名

namespace filesystem = std::filesystem;

这是一个演示程序

#include <iostream>

namespace A
{
namespace B
{
int x;
}
}

int main()
{
namespace B = A::B;

B::x = 10;

std::cout << B::x << '\n';

return 0;
}

它的输出是

10

关于c++ - using-declaration 不能命名命名空间,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63751519/

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