0

I am trying to write a function CycleManager which can hold an array of Cycles. My problem is that I want this class to be able to hold a variable amount of cycles. This is what I came up with, but it's not working.

Cycle.h

class Cycle
{
public:
    Cycle(std::string name) : name(name) {}

private:
    std::string name;
};

CycleManager.h

#include "Cycle.h"

class CycleManager
{
public:
    CycleManager(Cycle (&cycles)[]);
    void getCycles();

private:
    int currentCycle;
    int numberOfCycles;
    Cycle (&cycles)[];
};

CycleManager.cpp

CycleManager::CycleManager(Cycle (&cycles)[]) : cycles(cycles), currentCycle(0)
{
    numberOfCycles = sizeof(*cycles) / sizeof(Cycle);
}

void CycleManager::getCycles()
{
    // Serial.println(this->numberOfCycles);
    std::cout << this->numberOfCycles << std::endl;
}

Main.cpp

Cycle cycles[] = {Cycle("Cycle 1"), Cycle("Cycle 2"), Cycle("Cycle 3")};
CycleManager cycleManager(cycles);

int main()
{
    cycleManager.getCycles();
    return 0;
}
seddouguim
  • 504
  • 3
  • 12
  • Is using arrays instead of [`std::vector`](https://en.cppreference.com/w/cpp/container/vector) required? – MikeCAT Jul 13 '22 at 12:32
  • 4
    Use `std::vector`? – Jason Jul 13 '22 at 12:32
  • I'm working on an embedded system so can't use vectors. – seddouguim Jul 13 '22 at 12:39
  • 1
    Do you know what the largest size array you could possibly need? Use that upper limit to declare the array size, and just use less of the array's maximum capacity. – Eljay Jul 13 '22 at 12:40
  • @AnoopRana You closed to soon, the question is relevant since OP is on an embedded system and he cannot use std::vector. – Pepijn Kramer Jul 13 '22 at 12:47
  • 1
    I've had to work in an embedded system that had no heap, so I couldn't use `std::vector`. – Eljay Jul 13 '22 at 12:47
  • 1
    Probably you can still use std::array, it is a bit more friendly to use when you want to pass it around and/or return arrays from functions. @OP , Next time be a bit more clear about being in an embedded system, rules change a bit then :) – Pepijn Kramer Jul 13 '22 at 12:48
  • @PepijnKramer Note that here the OP says " it's not working." It is not clear, what do they mean by not working. Does it mean not compiling or runtime error or something else. This question is not clear at all. Additionally the dupes already explain that from C++20 we can pass array of known bound as argument to parameter of type reference to array of unknown bound. Moreover which version of C++ is the OP using. This question is lacking information and so(in its current state) is a duplicate of the linked dupes. – Jason Jul 13 '22 at 12:51
  • to get a limited-sized stack-based vector use `boost::container::static_vector`. See [Variable-length std::array like](https://stackoverflow.com/a/70850093/995714) – phuclv Jul 16 '22 at 04:34
  • and instead of `Cycle cycles[] = {Cycle("Cycle 1"), ...};` you should use [`std::initializer_list`](https://en.cppreference.com/w/cpp/utility/initializer_list) which provides the size, similar to how standard containers like [`std::vector`](https://en.cppreference.com/w/cpp/container/vector/vector) receive a variable list of inputs – phuclv Jul 16 '22 at 04:37

1 Answers1

1
Cycle cycles[] = {Cycle("Cycle 1"), Cycle("Cycle 2"), Cycle("Cycle 3")};
CycleManager cycleManager(cycles);

At this point you know the size of the array. So why not

CycleManager cycleManager(cycles, size);

Adjust constructor appropriately

CycleManager(Cycle* cycles, size_t n):cycles(cycles), numberOfCycles(n)...
Captain Giraffe
  • 14,407
  • 6
  • 39
  • 67