Page 1 of 1

Derived Classes error

Posted: 15 Mar 2018 01:51
by donic46
Hello,

The z-uno compiler give me error when i create the "changeValue" function on the Switch class.
Error: object_sdcpp_.cpp:747:1:warning:control reaches end of non-void functionUnknown error:'class Channel' uCxx returned error code:-1

If i comment the function "changeValue" in the object.cpp the code compile without errors.

The same code dont give any error with the arduino compiler.

Code:
object.h

Code: Select all

#ifndef Object_h
#define Object_h

#include "Arduino.h"

class Channel {
  public:
    Channel(uint8_t channel, int min ,int max);
  protected:
    uint8_t reportChannel;  // Position of channel in the Z-UNO channels
    int minValue;           // Min valid value
    int maxValue;           // Max valid value
};

class Switch : public Channel {
  public:
    Switch(uint8_t channel, int min, int max) : Channel(channel, min, max) { }
    
    uint8_t changeValue(int v);
  protected:
    int value;
};

class BinarySwitch : public Switch {
  public:
    BinarySwitch(uint8_t channel) : Switch(channel, 0, 1) { } 
  
};

class MultiSwitch : public Switch {
  public:
    MultiSwitch(uint8_t channel) : Switch(channel, 0, 99) { } 
  
};

#endif
object.cpp

Code: Select all

#include "object.h"

// CHANNEL

Channel::Channel(uint8_t channel, int min ,int max) {
  reportChannel = channel;
  minValue = min;
  maxValue = max;
}

// SWITCH

uint8_t Switch::changeValue(int v) {
  value = v;
}
Any suggestion?

Thanks

Re: Derived Classes error

Posted: 19 Mar 2018 01:50
by p0lyg0n1
Hi,
There are two errors. First one is that you have to return a value in Switch::changeValue. It have to be something like:

Code: Select all

// ...
uint8_t Switch::changeValue(int v) {
  value = v;
  return v;
}
Unfortunately the second error is inside the translator. Thanks to Your code, we were able to find an old bug in it. This involves with call of base constructor inside initialization list of derived class constructor. We have fixed it already in current alpha version of translator. We will publish it with version 2.1.4. At this moment you can't use code like this:

Code: Select all

Switch(uint8_t channel, int min, int max) : Channel(channel, min, max) { }
Cause the fields are protected you can do a workaround like this:

Code: Select all

Switch(uint8_t channel, int min, int max):  reportChannel(channel),  minValue(min),  maxValue(max){ }
It's not good, but it's a workaround.
I am not recommend to you to use deep OOP(Object Oriented Programming) approach here. This architecture haven't much stack for it. Try to use raw C approaches for more cases and use C++ features only when you really need them. Anyway your sample code is very good, cause we was able to find an old bug by means of it. Thank you!

Re: Derived Classes error

Posted: 19 Mar 2018 13:17
by donic46
No problem i am using this only to organize the code.

Thanks.