Go Down

Topic: Error using struct in Arduino C++ (Read 3570 times) previous topic - next topic

Harrzack


I'm attempting to use a struct for an Arduino project.  When I attempt to use objects defined by the struct, Visual Studio Intellisense is unhappy and flags them as unknown.  Enclosed pix shows the error.

From all I can find on the net this APPEARS to be the right way to use them - where am I stepping on my weenie?   :smiley-mr-green:
--------------------------------------------------------
Code: [Select]
struct Menu {
char name[16];
byte order;
byte state; // System State assgined to this menu is selected with RE switch
int display; // where 7-seg data for this menu item is held
byte ledpin;
};


Menu Count_menu;
Menu Angle_menu;
Menu Speed_menu;
Menu Clear_menu;
Menu Step_menu;
/*  the lines below are flagged as 'no storage class or type identifier' */
Count_menu.state = STATE_DATA;
Count_menu.ledpin = MLED_COUNT;
Angle_menu.state = STATE_DATA;
+++++++++++++++++++++++++++++++++++++
The completion date of a software project is 2 weeks from any given date.

KeithRB

try putting "struct" in front of your declarations. In C, you would need to create a typedef to not use the struct keyword.

Harrzack

Hmm - good idea but no go.  When I use the struct in front of the declarations, it gets unhappy with the '.'.

From what I've read, Arduino is c++ and Intellisense only gets bummed when I try to USE the items defined with the struct.
My basic layout SHOULD be ok...  i.e. not having to prefix the declarations.
+++++++++++++++++++++++++++++++++++++
The completion date of a software project is 2 weeks from any given date.

PaulS

If you really want help, you'll post ALL of your code.
The art of getting good answers lies in asking good questions.

Harrzack

Actually that WAS all the code - it was just a little test bit I was doing.

I've since found out that there are some... issues using a STRUCT in Arduino C++ - So I've abandoned that idea and just created an ADC and it works nearly the same and perhaps better.
I get the impression that STRUCT was a form from 'plain' C that didn't carry over so well to C++  -  and doing a class is nearly as easy.

=A.
+++++++++++++++++++++++++++++++++++++
The completion date of a software project is 2 weeks from any given date.

SirNickity

My approach is to typedef the struct all in one go:

Code: [Select]
typedef struct {
 int  a;
 int  b;
} MyType;

MyType  nerdy_redhead;


The label you use after the keyword "struct" is the struct's name, which can be omitted.  The label after the struct definition is the typedef'd alias, which can also be omitted.  You get your pick which to use.  My way is all I want 99.999% of the time, so I don't bother with the struct label.  If it makes you feel better, you can still include it though:

Code: [Select]
typedef struct MyType {
...
}  MyType_t;

MyType_t  pixie_brunette;


This way uses the _t convention to denote a "type", like uint8_t, etc.

PaulS

Quote
Actually that WAS all the code - it was just a little test bit I was doing.

Really? So, where is the compiler supposed to find values for STATE_DATA and MLED_COUNT?
The art of getting good answers lies in asking good questions.

majenko

If that is all your code, then the assignments you are doing are outside of a function and are invalid.

You can't do:
Code: [Select]

int a;
a = 3;

void setup() {
}

void loop() {
}


as the instruction "a = 3;" is a piece of code to be executed, and it can't be executed there.

Instead you put
Code: [Select]

int a = 3;

which is valid, or in your case
Code: [Select]

Menu Count_menu = {"", 0, STATE_DATA, 0, MLED_COUNT};

with 0 or "" being used for fillers for data I don't know about.

Harrzack

Thanks to all who replied.  YES - the DEFINES were not included in my sample...  They are correct and where not creating the problem I was having.  As I said - it appears that STRUCT's don't seem to work in Arduino C.   They define correctly, but when I attempt ANY form of assignment, it fails.  I read something about 'namespace' issues - it got sorta nasty.

In any case, creating and ADC for this is a better solution (maybe a tad more work) but works nicely in the end.

=Alan R.
+++++++++++++++++++++++++++++++++++++
The completion date of a software project is 2 weeks from any given date.

Harrzack

Further comments on this:  I'm working in Visual Studio 2012 with the Visual Micro plug-in.  In the included screen shot you can see how VS is complaining about the definition.  No amount of 'typedef' or 'struct' positioning would make it happy... so I gave up on STRUCT.   I do like the ADC's as a better solution anyway...

=Alan R.
+++++++++++++++++++++++++++++++++++++
The completion date of a software project is 2 weeks from any given date.

PaulS

Quote
As I said - it appears that STRUCT's don't seem to work in Arduino C.

You appear to be the only one having problems with them, and you posted code that doesn't compile, to "illustrate" the vaguely worded problem description. No one else can replicate your problem or see what the real errors, not your handwaving, are.
The art of getting good answers lies in asking good questions.

Harrzack

Sorry I offended you with my 'handwaving'...   the full code is in 5 files and would be a PITA for me to load and you to read.

Case closed - thanks for all the help. 
+++++++++++++++++++++++++++++++++++++
The completion date of a software project is 2 weeks from any given date.


Harrzack

Apologies to All! Something is def broke in my kludge program as I just made a tiny 'test-struct' program and it behaves as expected.  My dad always said there was no point in being stupid unless you could show it... :smiley-red:

+++++++++++++++++++++++++++++++++++++
The completion date of a software project is 2 weeks from any given date.

AWOL

+1 karma

Thanks for getting back.

Go Up