让我有些惊讶的是这个程序:
struct A {
virtual void a()=0;
};
struct B : public A {
void a() {}
};
int main() {
B b;
b.a(); // OK, call B::a()
b.A::a(); // linker error?
}
我遇到了这个错误(gcc 4.4):
/tmp/ccfOGuBJ.o: In function `main':
test.cc:(.text+0x28): undefined reference to `A::a()'
collect2: ld returned 1 exit status
(clang 7.0.0)
Undefined symbols for architecture x86_64:
"A::a()", referenced from:
_main in test-440cc5.o
ld: symbol(s) not found for architecture x86_64
clang: error: linker command failed with exit code 1 (use -v to see invocation)
我原本以为调用一个被声明为已删除的纯函数会产生显式错误,而不是隐式错误。标准中是否区分“不在此翻译单元中的函数”和“不在任何翻译单元中的函数”?
需要注意的是,在§10.4中没有直接调用纯虚函数的说明。
virtual void f() = 0 { }; // ill-formed
。这是正确的,但让我误以为纯虚函数不能有实现,而不是简单地混合这些语法不受支持。谢谢! - OmnipotentEntity