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;
}
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)...
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.