You can chat about native SDK questions and issues here.

User avatar
By Stevenelson
#90739 I'm struggling with some syntax in my class file. This isn't *that* important, but I'm trying to get better at OOP programming.

This code works, but I don't like that it has a global variable. I'd like to make the webSocket variable a private variable of this class.


Code: Select all#ifndef _BRAINSTEM_H_
#define _BRAINSTEM_H_

#include <Arduino.h>
#include <WebSocketsServer.h>
#include <Hash.h>

WebSocketsServer webSocket = WebSocketsServer(81);

class Brainstem{

    public:
        void begin();
    private:
       

};

void Brainstem::begin(){
 
}


This *seems* like it should work and it feels like it's more encapsulated as a private variable. But I'm getting compiling errors.

Code: Select all#ifndef _BRAINSTEM_H_
#define _BRAINSTEM_H_

#include <Arduino.h>
#include <WebSocketsServer.h>
#include <Hash.h>

class Brainstem{

    public:
        void begin();
    private:
        WebSocketsServer webSocket;

};

void Brainstem::begin(){
    webSocket = WebSocketsServer(81);
}


I'm not sure if all of this is wrong-headed thinking for this particular library. Maybe it's not designed to work like this. Is there a glaring flaw in my syntax or am I just thinking about this library wrong?
User avatar
By Pablo2048
#90740 The syntax is wrong IMO. Try to use:
Code: Select allclass Brainstem{

    public:
        void begin();
    private:
        WebSocketsServer *webSocket;
};

void Brainstem::begin(){
    webSocket = new WebSocketsServer(81);
}