[SOLVED]struct does not name a type (in .ino)

I declared a struct “xy8bit” in the public sections of the header file. Why is it not visible in a function in the .ino file? Code:

/*
LEDCube.cpp
*/
#include "Arduino.h"
#include "LEDCube.h"

LEDCube::LEDCube() {
  FastLED.addLeds<WS2811, 7>(leds, 343);
}

/*
other stuff
*/

void LEDCube::teststruct() {
  xy8bit testi[10];  //works
}
/*
LEDCube.h
*/
#ifndef LEDCube_H
#define LEDCube_H
#include "Arduino.h"
#include <FastLED.h>
#include <Adafruit_NeoPixel.h>
#define NUMLEDS 343
class LEDCube
{
  public:
  LEDCube();
  typedef struct xy8bit {
	uint8_t x;
	uint8_t y;
  };
  /*
  other stuff
  */
  void teststruct();
};
#endif
/*
LEDCube.ino
*/
#include <LEDCube.h>
#include <FastLED.h>
#include <Adafruit_NeoPixel.h>

int limit = 7;  //ENTER HERE YOUR LED NUMBER
int numLeds = limit * limit * limit;
int limitsqu = limit * limit;

int testvar;

LEDCube cube;

xy8bit testi; // throws 'xy8bit' does not name a type

void setup() {
  delay(500);
  Serial.begin(9600);
}

void loop() {
}

'typedef' is used to define a type, not declare a variable. Removing that keyword might help.

I am still getting the same error without “typedef”.

xy8bit testi; // throws 'xy8bit' does not name a type

The xy8bit type only lives inside the LedCube class where you defined it. Move the definition of that struct outside the class if you want to be able to create instances of it outside the class.

Delta_G: xy8bit testi; // throws 'xy8bit' does not name a type

The xy8bit type only lives inside the LedCube class where you defined it. Move the definition of that struct outside the class if you want to be able to create instances of it outside the class.

Now it works, thank you. But then I have xy8bit defined in the LEDCube.ino and LEDCube.h. Can I use them as they were the same?

Yes, you can define it there. There's no law that says that a file can only have definitions for things that match the name of the file.

Delta_G: Yes, you can define it there. There's no law that says that a file can only have definitions for things that match the name of the file.

Should have asked like that: I create an instance of xy8bit in the .ino and for example pass this instance as a parameter to a function that is in the .cpp. Can I treat the two definitions like that?

I don't understand what you are asking but I think the answer is, "try it and see what happens". I think you'll be just fine. If not it will throw an error and then we can see if we can solve it.

Delta_G:
I don’t understand what you are asking but I think the answer is, “try it and see what happens”. I think you’ll be just fine. If not it will throw an error and then we can see if we can solve it.

I tried this:

/*
LEDCube.ino
*/
#include <LEDCube.h>
#include <FastLED.h>
#include <Adafruit_NeoPixel.h>

int limit = 7;  //ENTER HERE YOUR LED NUMBER
int numLeds = limit * limit * limit;
int limitsqu = limit * limit;

typedef struct xy8bit {
  uint8_t x;
  uint8_t y;
};

LEDCube cube;

xy8bit test;

void setup() {
  delay(500);
  Serial.begin(9600);
}

void loop() {
  cube.loadLetter('G', test);  //What i tried
}
/*
LEDCube.h
*/
#ifndef LEDCube_H
#define LEDCube_H
#include "Arduino.h"
#include <FastLED.h>
#include <Adafruit_NeoPixel.h>
#define NUMLEDS 343
class LEDCube
{
  public:
  LEDCube();
  typedef struct xy8bit {
	uint8_t x;
	uint8_t y;
  };
  void loadLetter(char letter, xy8bit *setxy);  //Target function
};

And this throws the error: “no known conversion for argument 2 from ‘xy8bit’ to ‘LEDCube::xy8bit*’”

This did i want to ask if this would have worked which it obviously doesnt. How can I fix that?

hallolo: This did i want to ask if this would have worked which it obviously doesnt. How can I fix that?

the type xy8bit is a class member:

class LEDCube
{
  public:
  LEDCube();
  typedef struct xy8bit {
 uint8_t x;
 uint8_t y;
  };
  void loadLetter(char letter, xy8bit *setxy);  //Target function
};

so its use is limited to within the class.

you can try defining it in the header, outside and before the class definition; and remove what will be a duplicate definition in your .ino file.

BulldogLowell: the type xy8bit is a class member:

class LEDCube
{
  public:
  LEDCube();
  typedef struct xy8bit {
 uint8_t x;
 uint8_t y;
  };
  void loadLetter(char letter, xy8bit *setxy);  //Target function
};

so its use is limited to within the class.

you can try defining it in the header, outside and before the class definition; and remove what will be a duplicate definition in your .ino file.

Ok, thank you

BulldogLowell: so its use is limited to within the class.

You can use it outside the class if you use the full name LEDCube::xy8bit.

OK, in the code in #8 you actually have two things defined to have the same name. The one from the class isn't the same as the one you defined in the .ino. They just happen to have the same name. They actually don't have the exact same name because the one in the class is LEDCube::xy8bit

The other thing you could have done instead of defining the struct outside the class would have been to refer to it by that name instead. Use LEDCube::xy8bit instead of just xy8bit and you can leave it in the class. If you want to be able to refer to it everywhere as just xy8bit then you have to move it outside the class.

Which file it is in ins't germane. It's just about whether or not the thing is defined inside the class or not.