受保护的函数调用c ++

Rob*_*moo -2 c++ inheritance

Class Base() {
protected:
    void foo();
}
Class Derived : public Base {
    void bar();
}

void Derived::bar(){
    foo();    //this causes an error.
}
Run Code Online (Sandbox Code Playgroud)

我知道我可能错过了一些明显的东西,但我已经绕圈了一个小时.如何在派生类中调用受保护的函数?

mat*_*yce 5

注释中出现的错误是链接器错误,所以你检查过:

没有更多信息,很难再说出来了.


此外,您的代码包含无效语法,这会导致错误:

  • class 是小写的
  • 课名后没有括号
  • ; 课后定义

以下代码适用于(直到它到达链接器)g++ version 4.9.0:

class Base {
protected:
    void foo();
};

class Derived : public Base {
    void bar();
};

void Derived::bar(){
    foo();
}
Run Code Online (Sandbox Code Playgroud)

  • @Robomoo通过提供pseude-code来询问实际代码的问题并不真正起作用,通常......您如何期望伪代码显示问题?您如何期望答案者将伪问题与真正的问题区分开来? (4认同)