In my quest to understand classes better I am trying to create a class for some functions I have for displaying things on an OLED. The code I have does things like splitting the display into lines at the top for non-scrolling text with the remaining text as scrolling. What exactly my code does isn't so important as that it needs an array to hold the text that will be sent to the display. I thought using an array in a class would be simple...
Here are some of my attempts with the corresponding error messages:
class OLED_helper {
private:
bool requestDisplayUpdate;
uint8_t displayNumRows; // Total number of rows of text on the display
uint8_t displayNumCols; // Total number of columns on the display
uint8_t displayNumStaticRows; // Rows at the top reserved for static non-scrolling text, remaining rows used for scrolling text
char displayBuffer[displayNumRows][displayNumCols];
public:
OLED_helper(const uint8_t rows, const uint8_t cols, const uint8_t staticRows) : displayNumRows(rows), displayNumCols(cols), displayNumStaticRows(staticRows) {}
};
invalid use of non-static data member 'OLED_helper::displayNumRows'
I understand why this fails, because the compiler wants to know the size of the array at compile time, which it doesn't. I hope this example illustrates what I'm trying to do though.
I've gone through various attempts at modifying the above, my latest attempt is this:
class OLED_helper {
private:
bool requestDisplayUpdate;
uint8_t displayNumRows; // Total number of rows of text on the display
uint8_t displayNumCols; // Total number of columns on the display
uint8_t displayNumStaticRows; // Rows at the top reserved for static non-scrolling text, remaining rows used for scrolling text
char * displayBuffer;
public:
OLED_helper(const uint8_t rows, const uint8_t cols, uint8_t staticRows) {
displayNumRows = rows;
displayNumCols = cols;
displayNumStaticRows = staticRows;
displayBuffer = new char[rows][cols];
}
};
array size in new-expression must be constant
It seems to me that the array size is constant, but the compiler says otherwise and in my long running battle with the compiler the compiler has yet to lose.
Questions, suggestions, comments and advice most welcome please.