Someone with C++ experience here?

Anything not relating to the X-Universe games (general tech talk, other games...) belongs here. Please read the rules before posting.

Moderator: Moderators for English X Forum

Post Reply
User avatar
Tamina
Posts: 1522
Joined: Sun, 26. Jan 14, 10:56

Someone with C++ experience here?

Post by Tamina » Wed, 18. Apr 18, 13:23

I bought an Arduino and now I am trying to get my head around C++ - and it is a painful headache.
I would like to make some classes and call a method on them, without the need to distinguish them, but I do not know why it fails.
Maybe someone can help me with this?

Code: Select all

#ifndef RUNNABLE_H
#define RUNNABLE_H
#include "Arduino.h"

class Runnable {
  public:
    virtual boolean loop() = 0;
};

#endif

Code: Select all

#ifndef MOTOR_H
#define MOTOR_H
#include "Runnable.h"

class Motor : Runnable {
  private:
  
  public: Motor();
};

#endif

Code: Select all

#include "Motor.h"

Motor::Motor () {
}

boolean Motor::loop() {
  Serial.println( "LOOP" );
  return true; 
}
I got this from a tutorial online but the compiler tells me that there is a problem that loop is "pure virtual"?

Code: Select all

  /l、 
゙(゚、 。 7 
 l、゙ ~ヽ   / 
 じしf_, )ノ 
This is Tamina. Copy Tamina to your signature to help her achieve world domination.

Cycrow
Moderator (Script&Mod)
Moderator (Script&Mod)
Posts: 20534
Joined: Mon, 15. Nov 04, 00:26

Post by Cycrow » Wed, 18. Apr 18, 14:06

you are missing the function define in the class Motor

class Motor : Runnable {
private:

public: Motor();
boolean loop() override;
};

pjknibbs
Posts: 28316
Joined: Wed, 6. Nov 02, 21:31

Post by pjknibbs » Wed, 18. Apr 18, 16:16

Yeah, what he said. When you declare a function as virtual you're basically saying, "Not going to define this here, going to define it in inherited classes"--so you really need to define it in those inherited classes or the compiler won't be happy.

CBJ
EGOSOFT
EGOSOFT
Posts: 35997
Joined: Tue, 29. Apr 03, 00:56

Post by CBJ » Wed, 18. Apr 18, 16:26

pjknibbs wrote:When you declare a function as pure virtual you're basically saying, "Not going to define this here, going to define it in inherited classes"...
Small correction underlined. For the OP's benefit, that's the "= 0" bit in the declaration.

User avatar
Tamina
Posts: 1522
Joined: Sun, 26. Jan 14, 10:56

Post by Tamina » Wed, 18. Apr 18, 17:18

Thanks :D That works, and it is moving!! :D
I thought it is implicit when extending Runnable,

Code: Select all

Motor : Runnable
Motor is inheriting all methods from Runnable.

It seems redundant to explicitly reintroduce them in the header-file of Motor again.

Code: Select all

  /l、 
゙(゚、 。 7 
 l、゙ ~ヽ   / 
 じしf_, )ノ 
This is Tamina. Copy Tamina to your signature to help her achieve world domination.

CBJ
EGOSOFT
EGOSOFT
Posts: 35997
Joined: Tue, 29. Apr 03, 00:56

Post by CBJ » Wed, 18. Apr 18, 17:25

It's not redundant as far as the compiler is concerned, but the reasons for that are not something it's worth getting into if you're just trying to get your head around C++.

Post Reply

Return to “Off Topic English”