0

I already have this class

class sensor : iElectronicParts{
    // Some stuff here like methods and properties
};

For accessing all sensors I want to store them in a new object with type of global, so I just coded this class (there's no set method because I already ran into an error)

class global {
  public:
  sensor getSensors() {
    return sensors; // This line throws the error
  }

  private:
  sensor sensors[10];
};

and I got this error message

Compiling sketch...

/tmp/538691267/200119_Growbox/200119_Growbox.ino: In member function 'sensor global::getSensors()':

/tmp/538691267/200119_Growbox/200119_Growbox.ino:335:12: error: could not convert '((global*)this)->global::sensors' from 'sensor [10]' to 'sensor'

return sensors;

^~~~~~~

exit status 1

I did not instantiated this object yet, I only defined the class. If I replace every type definition of sensor by int it works.

Could someone please tell me what I'm doing wrong / what I have to do to fix this?

alve89
  • 971
  • 1
  • 9
  • 31
  • 2
    A `sensor` isn't the same thing as an array of `sensor`. – Blaze Jan 22 '20 at 08:29
  • While the now deleted answer gave the wrong solution, the problem was described correctly: An array is not the same as a single element of the array. While Arduino have their own library and "standard" classes, I still recommend that you invest and spend some time reading [a few good C++ books](https://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list/388282#388282). – Some programmer dude Jan 22 '20 at 08:50

2 Answers2

4

You are storing an array of sensor in your class, but GetSensors is trying to return a copy of a single sensor. There's no conversion from an array to a single sensor, so your compiler doesn't know what it needs to do.

The question here is what you want to do: Do you want the whole list, or do you want to get a single sensor.

If You want to return the whole array.

C++ does not support arrays as return types. You will have to return a pointer to the first element in your array.

The code would look like this:

  sensor* getSensors() {
    return sensors;
  }

and you would use it like this, assuming g is an instance of global:

sensor* sensors = g.getSensors();
Serial.print(sensors[3].value);

This code uses what is called array to pointer decay. Callers of your getSensors function will no longer know what the size of the array is, you have to make sure to not go over bounds! As this returns a pointer to the original sensors, any modifications that you do later to the returned pointer will affect the array in the instance of global.

If you want to return a specific sensor

You just need to change your getSensors function to take as an argument the index of the sensor that you care about. It would look like this:

sensor& getSensor(int index) {
    return sensors[index];
  }

This returns a reference to a specific sensor. As with the pointer case, changes to the returned sensor will affect the original array inside the instance of global (which I assume is the intent)

If you do not want to make modifications to the original array, you should return by value, and omit the & from the return type.

divinas
  • 1,787
  • 12
  • 12
1

getSensors() function's return type is a mismatch. you should return a sensor object then have to choose one of the sensors[index] or change the return type to sensor pointer. however, I advice to use stl types (you do not have to but it is the best way) @divinas has inform me that no std::array in ardunio so this advice is not applicable:

#include <memory>
#include <array>
class iElectronicParts{};

class sensor : iElectronicParts{
    // Some stuff here like methods and properties
};

class global {
  public:
    std::array<sensor, 10>& getSensors() {
    return sensors; 
  }

  private:
  std::array<sensor, 10> sensors;
};

int main(){
    return 0;
} 
Semih Kekül
  • 399
  • 3
  • 14