gpt4 book ai didi

c++ - 模板继承会影响继承的成员

转载 作者:搜寻专家 更新时间:2023-10-31 02:09:44 26 4
gpt4 key购买 nike

<分区>

我遇到了最奇怪的错误,我不知道我做错了什么。

template <bool X>
struct A {
int x;
};

template <bool X>
struct B : public A<X> {
B() { x = 3; } // Error: 'x' was not declared in this scope.
};

我不明白为什么我无法从 B 中看到 x,因为我公开继承了 A

同时,这段代码编译:

template <bool X>
struct A {
int x;
};

template <bool X>
struct B : public A<X> {};

int main() {
B<false> b;
b.x = 4;
};

我正在使用 g++ 7.0.1 进行编译。

编辑:似乎如果我引用 x 的全名,代码就会编译,如:

B() { A<X>::x = 3; }

但为什么呢?

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