gpt4 book ai didi

c++ - 用最近定义的结构替换前向声明的结构

转载 作者:太空宇宙 更新时间:2023-11-03 17:25:17 25 4
gpt4 key购买 nike

我正在尝试用最近定义的结构 A 替换前向声明的结构 B。这是一个代码示例:

#include <iostream>
using namespace std;

namespace n1 {
struct B;

namespace n3 {
void func(B& b) {
cout << "b\n";
}
}

}

namespace n2 {
struct A{};
}

namespace n1 {
using B = n2::A;
}

int main() {
n1::B b;
n1::n3::func(b);
return 0;
}

出现以下错误:

conflicting declaration ‘using B = struct n2::A’
using B = n2::A;
^
prog.cpp:5:8: note: previous declaration as ‘struct n1::B’
struct B;

即使合法,这种技巧是否可能以某种方式实现?我错过了什么吗?

最佳答案

您的 using 指令正在为 n2::A 创建别名,名称 n1::B 已被结构声明 使用>struct B; inside namespace n1 (它不是 struct B 的另一个声明,它是名称别名)。您可能尝试实现的是提供您的 n1::B 结构的定义,可以像这样完成:

namespace n1
{
struct B {
// definition here, you might use your struct A if you need
};
}

namespace docs

关于c++ - 用最近定义的结构替换前向声明的结构,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59489399/

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