简体   繁体   English

指向虚拟课程的指针

[英]Pointers to virtual classes

Recently, I stumbled on another C++ problem that gives me quite some hard time. 最近,我偶然发现了另一个让我筋疲力尽的C ++问题。 Suppose we have the small program: 假设我们有一个小程序:

class A {
  public:
    virtual bool c() = 0;
    virtual bool b() = { return false; };
    virtual ~A() {}
}

class B : public A {
   public:
     bool b() = { return true; };
     ~B() {}
}

...

void c(A *pointer) {
  if (pointer->b()) {
    cout << "Derived class";
  }
}

In this case, the compiler returns error on the "if" line of the method c() with the error "member access into the incomplete type A". 在这种情况下,编译器在方法c()的“if”行返回错误,错误为“成员访问不完整类型A”。 Can somebody explain me why the compiler is returning such error? 有人可以解释一下为什么编译器会返回这样的错误吗? And if it is indeed right in firing an exception, then how can I prevent it? 如果它确实是正确的解雇异常,那么我该如何防止呢?

Thanks very much! 非常感谢!

"Incomplete type A " means that in the code you're compiling (but not the code you posted), there isn't a definition of A before it's used in c . “不完整的类型A ”意味着在您编译的代码中(但不是您发布的代码),在c使用之前没有A的定义。 You will need the definition either in the same source file as c , or in a header included from that source file. 您将需要在与c相同的源文件中或在该源文件中包含的标头中定义。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM