C++ Class Expertise needed!

Ok, so the following code (snippet) works fine if compiled on Arduino but it fails if compiled locally or using Particle CLI:

class RGB {
private:
    uint16_t col;
public:
    uint8_t red;
    uint8_t green;
    uint8_t blue;

    RGB(uint8_t r, uint8_t g, uint8_t b);
    RGB();

    void setColor(int32_t r, int32_t g, int32_t b);

    RGB convert565toRGB(uint16_t color);
    uint16_t convertRGBto565(RGB color);
    uint16_t convertTo565();
};

class PixelsBase {
protected:
    /* device physical dimension in portrait orientation */
    int16_t deviceWidth;
    int16_t deviceHeight;

    /* device logical dimension in current orientation */
    int16_t width;
    int16_t height;

    boolean landscape;

    uint8_t orientation;

    boolean relativeOrigin;

    /* currently selected font */
    prog_uchar* currentFont;

    RGB* foreground;
    RGB* background;
...

The error occurs as the first RGB* foreground; giving “error: ‘RGB’ does not name a type”. I can understand that declaring an OBJECT of a class inside the same class is not allowed, I don’t understand why the function declaration is not working. Moreso, why does it work on Arduino!?

Are the 2 classes declared in the same file? I had problems with that on the IDE a while a go

@marcus, yes, they are in the same file.

Just a dumbo question, the file is a header file and not an .ino with its preproc weirdness :wink:

@ScruffR, it is a header file.

Well, I am not the guru but I remerber trying somthing like this a while a go. And I had to put the classes in different libraries. Why not give it a try :wink:

@marcus, what do you mean “different library”. Did you mean, different files?

@peekay
in the IDE, define a different lib. The ide stores the classes “lib’s” in different directories. I learned this when I received a backup of my code from Particle. The application is stored in a top dir, and all lib’s are stored in different subdir’s. containing the .h and .cpp file

@marcus, I think I understand what you mean. I’ll split the two classes into two sets of .cpp/.h files and try that. :wink:

let me know the result!

1 Like

Paul, I’m puzzled too,but try this


typedef class RGB {
  ...
} rgb_t;

class PixelsBase {
  ...
  rgb_t* foreground;
}

This at least built on Web IDE.

@ScruffR,
Some info about class versus structs:
typedef Specifier
enter link description here
The empty declarations compile, but the following code gives the same errors as @peekay saw.

typedef class RGB {
int var;
public:
void f(){};
private:
void q(){};
} rgb_t;

class PixelsBase {

rgb_t* foreground;
foreground->f(); //error
foreground->var =0; //error
};

I’m not an expert either, but my first thought (from experience) was to break them into separate files as well.

I split the RGB class def into RGB.h and the class functions into RGB.cpp, fixed the includes all around and I still get the same errors. The RGB class is just not getting defined for some reason.

Hold a min, there already is an RGB object (RGB.control() …) in the Particle world - is this getting in your way (with an obscure error message tho’)?

@peekay123, if you wrap your lib in a namespace WhatEver and use WhatEver::RGB* foreground; or use namespace WhatEver any ambiguities should be resolved.

typedef class RGB {
  int var;
public:
  void f(){};
private:
  void q(){};
} rgb_t;

class PixelsBase {
  rgb_t* foreground;
  //foreground->f(); //error <-- this is not allowed in a class definition
  //foreground->var =0; //error <-- ditto
};

Actually no, these are not the same error messages as Paul was seeing.
You, are putting "active code" (function call and pointer deref) where only declarations are allowed.

It's not a class vs. struct thing, the reason why the typedef works is because it also resolves above ambiguity.

@ScruffR,

you are correct, I fiddled a bit further with the code and this compiles :blush:

typedef class RGB {

  public:
    void f();
    int var;
  private:
    void q();
} rgb_t;

void rgb_t::f(){
};
  
void rgb_t::q(){};

class PixelsBase {
    public:
      rgb_t* foreground;
};

PixelsBase pb;

void setup() {
    pb.foreground->f(); 
    pb.foreground->var =0; 
}

void loop() {

}

@peekay123,

I did what you did too, and the solution is that RGB in capitals is apparently a name used by the firmware, I tried ‘rgb’ with small letters, and that compiled.

I checked the documentation and voila!
https://docs.particle.io/reference/firmware/photon/#rgb
RGB is an existing library.

@marcus, @ScruffR, I had done a search for the RGB stuff in the firmware and totally missed it!! When I renamed the entire RGB class and references to RGBp instead, it all compiled. Thanks for your help folks! This community rocks as usual.

BTW, the code I am porting is for the Pixels graphics library :smile:

2 Likes

Those are LED’s too huh, :smiley:, What’s in a name!