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

void Derived::bar(){
    foo();    //this causes an error.
}

I know i'm probably missing something obvious but I've been going round in circles for an hour. How do I call a protected function in a derived class?

Robomoo
  • 57
  • 1
  • 5
  • What error does it cause? – dlf Sep 09 '14 at 18:03
  • have you tried Base::foo()? – Javi Sep 09 '14 at 18:04
  • Note that the bar() method in Derived class is private, since that is the default visibility for a method in a class with no access specifiers. – Jeremy Friesner Sep 09 '14 at 18:05
  • I have tried Base::foo() and this->foo(), neither of which work. causes this error:Error 1 error LNK2019: unresolved external symbol "protected: unsigned int __thiscall ShaderProgram::LoadShader(unsigned int,class std::basic_string,class std::allocator > const &)" (?LoadShader@ShaderProgram@@IAEIIABV?$basic_string@DU?$char_traits@D@std@@V?$allocator@D@2@@std@@@Z) referenced in function "public: __thiscall BasicProgram::BasicProgram(void)" – Robomoo Sep 09 '14 at 18:05
  • from: class BasicProgram : public ShaderProgram – Robomoo Sep 09 '14 at 18:07
  • 1
    Sounds like you've neglected to add whatever .cpp file contains the definition of `Base::foo()` to your project file (or whatever the equivalent is for your build tool) – dlf Sep 09 '14 at 18:09
  • Hard to figure out with the code sample you gave. – Javi Sep 09 '14 at 18:17
  • 1
    -1 for not including the error, and for not posting actual code. You assumed that your problem was related to the method being `protected`, but you were wrong. – John Dibling Sep 09 '14 at 18:39

2 Answers2

5

The error that appears in the comments is a linker error, so have you checked that:

It's hard to tell any more without more info.


Also, your code contains invalid syntax, which causes error(s):
  • class is lower case
  • No brackets after class name
  • ; after class definition

The following code works (until it gets to the linker) on g++ version 4.9.0:

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

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

void Derived::bar(){
    foo();
}
Community
  • 1
  • 1
matsjoyce
  • 5,744
  • 6
  • 31
  • 38
  • 4
    @Robomoo Asking about problems with actual code by providing pseude-code does not really work, usually... How do you expect the pseudo-code to show the problem? How do you expect the answerers to distinguish pseudo-problems from the real problem? – hyde Sep 09 '14 at 18:13
4

The problem is that you are missing the foo() implementation. In addition to the syntax errors commented by other users and the public statement. The following code compiles.

#include <iostream>

class Base {
protected:
    void foo() {std::cout << "Hi there" << std::endl;}
};

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

void Derived::bar(){
    foo();    //this causes an error.
}

int main (int argc, char** argv){
    std::cout << "Hello world" << std::endl;
    Derived d;
    d.bar();

    return 0;
}
Javi
  • 3,440
  • 5
  • 29
  • 43