这里有一些人为编写的示例代码:
注意在错误中它提到了
这似乎表明继承自
(当然,这个问题可以通过对
以下是示例代码:
template<typename T> void Do(T arg) { (void)arg->b; }
namespace A {
struct Foo { int a; };
}
namespace B {
struct Foo { int b; };
struct Bar : A::Foo {
void Blah() { Do((Foo *)0); }
};
}
使用gcc 4.8.2编译(clang会出现类似的错误):
namespacebug.cpp: In instantiation of ‘void Do(T) [with T = A::Foo*]’:
namespacebug.cpp:10:34: required from here
namespacebug.cpp:1:39: error: ‘struct A::Foo’ has no member named ‘b’
template<typename T> void Do(T arg) { (void)arg->b; }
^
注意在错误中它提到了
T = A::Foo
,即使在调用处我正在创建一个位于命名空间B
内的Foo
。如果我删除基类声明(: A::Foo
),那么所有内容都会编译通过。这似乎表明继承自
A::Foo
会以某种方式将其带入我的命名空间,并将其与我的Foo
匹配使用?是哪个C++ "特性"导致了这种情况?(当然,这个问题可以通过对
Foo
使用命名空间轻松解决,但这不是问题的关键。)
(void)arg->b;
- 这是什么意思? - Ed Heal