Derived Classes error

Discussion about Z-Uno product. Visit http://z-uno.z-wave.me for more details.
Post Reply
donic46
Posts: 18
Joined: 05 Feb 2018 17:54

Derived Classes error

Post 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

p0lyg0n1
Posts: 181
Joined: 04 Aug 2016 07:14

Re: Derived Classes error

Post 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!

donic46
Posts: 18
Joined: 05 Feb 2018 17:54

Re: Derived Classes error

Post by donic46 »

No problem i am using this only to organize the code.

Thanks.

Post Reply