0

I have 2 files I am working on: encoder.h and encoder.cc.

As far as I know the inherited methods (initialize and read) should be available in the MotorEncoder class. However when I try to implement the methods the compiler throws an error. Any ideas?

Header

class Encoder {
protected:
    u32 bitResolution;
    SPI spi;

public:
    void initialize(u16 spiDeviceID, u32 bitResolution);
    u32 read();
};

class MotorEncoder : public Encoder {
public:
// If I comment these 2 lines it SHOULD work as far as I know, 
// but it won't compile due to the method not being defined when 
// implementing e.g. in MotorEncoder::initialize()
void initialize(u16 spiDeviceID, u32 bitResolution);
u32 read();
};

Implementation

void Encoder::initialize(u16 spiDeviceID, u32 bitResolution) {
    // ....
}

u32 Encoder::read() {
    //
}


void MotorEncoder::initialize(u16 spiDeviceID, u32 bitResolution) {
    // implementation
}

u32 MotorEncoder::read() {
    // implementation code
}
4
  • You probably need to declare initialize and read as virtual in class Encoder Commented Mar 15, 2016 at 11:20
  • initialize- and read- methods should be virtual in base class. Commented Mar 15, 2016 at 11:20
  • In C/C++ langauge, the default binding is static/compile-time. You will need to specify runtime-binding of member-function, using virtual keyword. The base and derived function signature must match. Commented Mar 15, 2016 at 11:45
  • @Ajay: should match - it's technically not an error if they are different. You just get two unrelated functions as a result. That's why there's override - that will turn a signature mistake in the derived class into an error. Always use it when deriving from a base class from a library. Commented Mar 15, 2016 at 13:26

1 Answer 1

3

Your methods in base and derived classes currently are unrelated. They just happen to have the same name.

The complete call expression would be this->Encoder::read() and this->MotorEncoder::read(). You may leave out this->, and unqualified read() would refer to MotorEncoder::read(), but they're still two functions.

That said, I think you have a more fundamental misunderstanding of inheritance. You don't need to declare or define MotorEncoder::read, precisely because it's already inherited. You may also want to read up on virtual.

initialize is another worrying sign. In C++, initialization is done by constructors.

Sign up to request clarification or add additional context in comments.

9 Comments

What we want to do is simply override the read and initialize methods of the base class. So that's why the two lines declaring these methods in MotorEncoder should not be there; it should be enough to override them in the cc file. (We are writing code later to be used on an FPGA, which is why dynamic allocation will not be supported and we are not using constructors.)
@Ortix92: "it should be enough to override them in the cc file" Where did you hear that?
@BarryTheHatchet the fields are inherited without redeclaration so why do we need to redeclare the methods? They should exist in the child class, am I correct? And I should be able to declare them as an override in the implementation, or not? I just don't understand why I have to redeclare the exact same method in the class if I'm inheriting these methods.
@Ortix92: No. The base class member functions are "inherited" but you can't define them twice. To create an override, you must first declare the override. It's not "the exact same method". Base::foo() is a different function from Derived::foo(), even though the latter overrides the former. And if you don't need an override then you just use Base::foo() throughout.
@Ortix92: That's my best guess on this issue : Imagine you are building a library. The only things your users see are your header files. Then, if you don't redeclare your derived functions in your header files, the users have no way to know in advance that you derived your function. He would only know at runtime, which is awfull. That's why the laguage imposes to redeclare the derived functions in the header files of the derived classes.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.