Help with my first library extending the Adafruit_SSD1306 oled screen library

Hello,
I am making a simple library called scroll which makes it easy for someone to initialize an adafruit oled screen and make text scroll accross and up and down the screen (A functionality which is not well executed in the adafruit library)
I made a class called

Scroll

Which should take five pin numbers and then create an Adafruit_SSD1306 object.

In Scroll.h I have

#include <Arduino.h> 
 
 
class Scroll{ 
public: 
     Scroll(int oled_dc, int oled_cs, int oled_clk, int oled_mosi, int oled_reset);
    void init(); 
    void startScroll(); 
    void setMessage(char* msg); 
     
private:  
    typedef enum {SIDEWAYS, UPWARDS} scroll_type; 
    struct  
    {   
        scroll_type scrolling;  
        int text_size; 
    }configuration; 
    char _message[200]; 
    int _x, _minX, _y, _minY; 
    void convert_comma_to_newline(char* msg); 
    int get_newlines(char* msg); 
    void setMinX(); 
    void setMinY();
};

in Scroll.cpp I have

#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>
#include "Scroll.h"

    
Scroll :: Scroll(int oled_mosi, int oled_clk, int oled_dc, int oled_reset, int oled_cs){
    Adafruit_SSD1306 disp(oled_mosi, oled_clk, oled_dc, oled_reset, oled_cs);
}  
void Scroll :: init(){
    configuration  = {UPWARDS,2};
    disp.begin(SSD1306_SWITCHCAPVCC);
    disp.setTextSize(configuration.text_size);
    disp.setTextColor(WHITE);
    _x = disp.width();
    _y = disp.height();
    if(configuration.scrolling == SIDEWAYS){
        disp.setTextWrap(false);
    }else{
        disp.setTextWrap(true);
    }
    setMinX();
    setMinY();
}

void Scroll :: startScroll(){
    disp.clearDisplay();
    if(configuration.scrolling == SIDEWAYS){
        disp.setCursor(_x, 20);
        if(--_x < _minX) _x = disp.width();
    }
    if(configuration.scrolling == UPWARDS){
        disp.setCursor(0, _y);
        if(--_y < _minY) _y = disp.height();
    }
    disp.print(_message);
    disp.display();
}
void Scroll :: setMessage(char* msg){
    int length = strlen(msg);
    strncpy(_message, msg, 200);
    if(configuration.scrolling == UPWARDS){
        convert_comma_to_newline(_message);
    }

The code works if I put the Adafruit_SSD1306 object outside of the constructor with predefined values but now when I try to create a Scroll object using

Scroll scrollscreen(9, 10, 11, 13, 12);

it tells me that disp was not declared in the scope. How can I make the disp i created in the constructor available to the rest of the Scroll.cpp without defining it in Scroll.h?

Also what is bad about this code and what could i do to make it better?

You need to construct the disp object at the same level as your constructor, not inside your constructor. What you've done there is to create a local variable called disp that goes out of scope when the constructor ends.

Put:

Adafruit_SSD1306 disp;

in the private section.

Then, change the constructor:

Scroll :: Scroll(int oled_mosi, int oled_clk, int oled_dc, int oled_reset, int oled_cs), disp(oled_mosi, oled_clk, oled_dc, oled_reset, oled_cs)
{
}

Now I get

Scroll.cpp:7: error: declaration of 'Scroll::Scroll(int, int, int, int, int)' outside of class is not definition [-fpermissive]
Scroll.cpp:7: error: expected constructor, destructor, or type conversion before '(' token

Now I get

to go to http://snippets-r-us.com

Thanks for your help
heres Scroll.h

#include <Arduino.h>


class Scroll{
public:
    Scroll(int oled_dc, int oled_cs, int oled_clk, int oled_mosi, int oled_reset);
    void init();
    void startScroll();
    void setMessage(char* msg);
    
private: 
    typedef enum {SIDEWAYS, UPWARDS} scroll_type;
    struct 
    {
        scroll_type scrolling; 
        int text_size;
    }configuration;
    char _message[200];
    int _x, _minX, _y, _minY;
    void convert_comma_to_newline(char* msg);
    int get_newlines(char* msg);
    void setMinX();
    void setMinY();
    Adafruit_SSD1306 disp;
};

heres Scroll.cpp

#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>
#include "Scroll.h"


Scroll :: Scroll(int oled_mosi, int oled_clk, int oled_dc, int oled_reset, int oled_cs), disp(oled_mosi, oled_clk, oled_dc, oled_reset, oled_cs){
}
void Scroll :: init(){
    configuration  = {UPWARDS,2};
    disp.begin(SSD1306_SWITCHCAPVCC);
    disp.setTextSize(configuration.text_size);
    disp.setTextColor(WHITE);
    _x = disp.width();
    _y = disp.height();
    if(configuration.scrolling == SIDEWAYS){
        disp.setTextWrap(false);
    }else{
        disp.setTextWrap(true);
    }
    setMinX();
    setMinY();
}

void Scroll :: startScroll(){
    disp.clearDisplay();
    if(configuration.scrolling == SIDEWAYS){
        disp.setCursor(_x, 20);
        if(--_x < _minX) _x = disp.width();
    }
    if(configuration.scrolling == UPWARDS){
        disp.setCursor(0, _y);
        if(--_y < _minY) _y = disp.height();
    }
    disp.print(_message);
    disp.display();
}
void Scroll :: setMessage(char* msg){
    int length = strlen(msg);
    strncpy(_message, msg, 200);
    if(configuration.scrolling == UPWARDS){
        convert_comma_to_newline(_message);
    }
    setMinX();
    setMinY();

}
void Scroll :: convert_comma_to_newline(char* msg){
    int length = strlen(msg);
    for(int i =0; i<length; i++){
        if((byte)msg[i] == 44){
            msg[i] = ' ';
            if(msg[i+1] == 44){
                msg[i] = '\n';
            }
            msg[i+1] = '\n';
            
        }
    }
}
int Scroll :: get_newlines(char* msg){
    int length = strlen(msg);
    int newlines = 0;
    for(int i = 0; i<length; i++){
        if((byte)msg[i] == 10){
            newlines++;
        }
    }
    return newlines;
}

void Scroll :: setMinX(void){
    _minX = -(configuration.text_size*6) * strlen(_message);   // 12 = 6 pixels/character * text size 2
}
void Scroll :: setMinY(void){
    _minY= -((get_newlines(_message))*(configuration.text_size*7))-disp.height();
}

And the error:

Scroll.cpp:7: error: declaration of 'Scroll::Scroll(int, int, int, int, int)' outside of class is not definition [-fpermissive]
Scroll.cpp:7: error: expected constructor, destructor, or type conversion before '(' token

OK. I see what the problem is. It’s right there in your thread title. You want your class to derive from, not to extend, the Adafruit_SSD1306 oled screen library.

class Scroll : public Adafruit_SSD1306
{ // Down here where the damned thing belongs
Scroll :: Scroll(int oled_mosi, int oled_clk, int oled_dc, int oled_reset, int oled_cs) : 
      Adafruit_SSD1306(oled_mosi, oled_clk, oled_dc, oled_reset, oled_cs)
{ // Down here where the damned thing belongs
}

And get rid of disp.
L:earn to bang the enter key now and then!