EnglishGermanFrenchRussianItalianSpanish
Log inRegister
 
Someone with C++ experience here?
Post new topic Reply to topic
View previous topic :: View next topic
Author Message
Tamina





Joined: 26 Jan 2014



PostPosted: Wed, 18. Apr 18, 13:23    Post subject: Someone with C++ experience here? Reply with quote Print

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:

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

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

#endif


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

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

#endif


Code:
#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:
  /l、
゙(゚、 。 7
 l、゙ ~ヽ   /
 じしf_, )ノ
This is Tamina. Copy Tamina to your signature to help her achieve world domination.
Back to top
View user's profile Send private message
Cycrow
Moderator (Script&Mod)
Moderator (Script&Mod)



Joined: 15 Nov 2004
Posts: 20521 on topic
Location: London
Thank you for registering your game
PostPosted: Wed, 18. Apr 18, 14:06    Post subject: Reply with quote Print

you are missing the function define in the class Motor

class Motor : Runnable {
private:

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


_________________
My Scripts | MY X3TC Scripts | X3 Plugin Manager | Custom Gui
Back to top
View user's profile Send private message Visit poster's website MSN Messenger
pjknibbs



MEDALMEDALMEDAL

Joined: 06 Nov 2002
Posts: 28271 on topic
Location: UK
Thank you for registering your game
PostPosted: Wed, 18. Apr 18, 16:16    Post subject: Reply with quote Print

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.

Back to top
View user's profile Send private message
CBJ
EGOSOFT
EGOSOFT



Joined: 29 Apr 2003
Posts: 35868 on topic

Thank you for registering your game
PostPosted: Wed, 18. Apr 18, 16:26    Post subject: Reply with quote Print

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.

Back to top
View user's profile Send private message
Tamina





Joined: 26 Jan 2014



PostPosted: Wed, 18. Apr 18, 17:18    Post subject: Reply with quote Print

Thanks Very Happy That works, and it is moving!! Very Happy
I thought it is implicit when extending Runnable,
Code:
Motor : Runnable

Motor is inheriting all methods from Runnable.

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


_________________
Code:
  /l、
゙(゚、 。 7
 l、゙ ~ヽ   /
 じしf_, )ノ
This is Tamina. Copy Tamina to your signature to help her achieve world domination.
Back to top
View user's profile Send private message
CBJ
EGOSOFT
EGOSOFT



Joined: 29 Apr 2003
Posts: 35868 on topic

Thank you for registering your game
PostPosted: Wed, 18. Apr 18, 17:25    Post subject: Reply with quote Print

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++.

Back to top
View user's profile Send private message
Display posts from previous:   
Post new topic Reply to topic
 
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum
You cannot attach files in this forum
You cannot download files in this forum
Control Panel
Login Data
The time now is Tue, 21. Aug 18, 05:47

All times are GMT + 2 Hours

[ Disclaimer / Impressum ] | [ Privacy Policy / Datenschutz ]

Board Security

Copyright © EGOSOFT 1989-2018
Powered by phpBB © 2001, 2005 phpBB Group
Template created by Avatar & BurnIt!
Debug: page generation = 0.09686 seconds, sql queries = 20