Code compilation error

Hello,

I am a Java developer new to C/C++ environment for Arduino. I searched a lot in this forum for similar error but did not find a solution…

Here is my problem : my code (following) is compiling well in Atmel Studio 6 but not in Arduino IDE, that is still complaining : “error: expected constructor, destructor, or type conversion before ‘*’ token”

#include <SD.h>
#include <XBee.h>

struct Alert {
  String addr;
  String pin;
  String comp;
  float val;
  // pour la liste chainee
  Alert * suivant;
};
// !!!!!!!! error indicated here !!!!!!!!!!!!!!!
// liste chainee
Alert* llist = NULL;

XBee xbee;
ZBRxIoSampleResponse ioSample;

File dataFile;

char DATA_FILE[] = "xbeedata.txt"; 
char CONFIG_FILE[] = "config.txt"; 
char SYNC_ORDER = 'y';
char CONFIG_ORDER = 'c';

const byte S_COMMAND = 8;
const byte S_XBEE = 7;
const byte S_NONE = 9;
byte CUR_STATE;

void setup() { 
  CUR_STATE = S_NONE;
  xbee = XBee();
  ioSample = ZBRxIoSampleResponse();
  xbee.begin(9600);
  Serial.begin(9600);
  ...
}
void loop() {
...
}
...

I read about common errors of initialization/assignment outside of methods but I really can’t understand what am I doing wrong…?

And even if I have these errors in Arduino IDE, can I upload my code on the board with Atmel (and its optimizations) ?

Thanks for your help.

  Alert * suivant;

What is an Alert? The compiler is telling you that it is clueless, and that, therefore, you have made a mistake.

okay, but I am trying to define a structure usable as a chained list (helped by some c++ tutorials). How can I do that in a different way? (sorry for this maybe "too-noob" question but in Java it seems easier :blush:...)

memel182:
okay, but I am trying to define a structure usable as a chained list (helped by some c++ tutorials). How can I do that in a different way? (sorry for this maybe "too-noob" question but in Java it seems easier :blush:...)

You are defining an object of type "struct Alert". The pointer needs to be of the same type, not some different type, if I understand what you are trying to do.

Wow I am really lost… I followed tutorials like this one http://www.cprogramming.com/tutorial/lesson15.html and I do not see what is different in my code… :sweat_smile:

I thought my “Alert” was like the “node” in the example… Am i wrong?

I thought my “Alert” was like the “node” in the example… Am i wrong?

Yes and no. The simple way to test which is the most accurate is to change your struct definition to:
struct Alert {
String addr;
String pin;
String comp;
float val;
// pour la liste chainee
struct Alert * suivant;
};
and see what happens.

By the way, lose the Strings.

Still the same error with this modification...

I tried to simplify my code and test compilation again to see if the compiler might have problems elsewhere but this error is recurrent.

One more thing to try. Put the struct definition in a .h file, and include that. The IDE may be adding stuff to your code to help you when that help really isn't all that helpful.

Did you fix this too?

struct Alert* llist = NULL;

Pete

Seems to be better (thank you for your time!!) but not really understandable : I created a .h file like you indicated, beside my original script, and more errors popped. But when I put the .h file in "libraries" folder of Arduino installation, it worked! Compilation succeeded (a new tab appeared in editor with my .h file)

But what do I have to understand? Isn't it possible to have my .h file beside my script? Not really helpful in file management (with Git here).

And why would this .h file be mandatory? (maybe my code is too long)

Finally, it might be more secure to keep coding with Atmel Studio?

Modifying your original code so it doesn't need external libraries, it compiled without errors for me.

struct Alert {
  String addr;
  String pin;
  String comp;
  float val;
  // pour la liste chainee
  Alert * suivant;
};
// !!!!!!!! error indicated here !!!!!!!!!!!!!!!
// liste chainee
Alert* llist = NULL;

void setup () { }
void loop ()  { }

What version of the IDE are you using?

I am using 1.0.4 (and tried with el_supremo suggestion too)

I created a .h file like you indicated, beside my original script, and more errors popped. But when I put the .h file in "libraries" folder of Arduino installation, it worked! Compilation succeeded (a new tab appeared in editor with my .h file)

Something is not right, here. You should not see a new tab if you create a file in a folder in the libraries directory. You only see a new tab if you create the file in the same directory as the sketch.

By creating a .h file, and including that, you added a #include statement. If you look at where the IDE adds code to your sketch, you may see why the struct definition in the sketch doesn't work, but the struct definition in the .h file does.

You should not see a new tab if you create a file in a folder in the libraries directory. You only see a new tab if you create the file in the same directory as the sketch.

You are right, I re-tested this.

The generated code is (seen in temporary folder) :

#line 1 "myfile.ino"
#include <SD.h>
#include <XBee.h>
#include <Biospy.h>

// liste chainee
#include "Arduino.h"
void setup();
void readConf();
void initSD();
void loop();
void commandMode();
void writeConfigurationFile();
void dumpDataFile();
Alert* findByAddress(String addr);
void xBeePacket();
#line 8
Alert* llist = NULL;

XBee xbee;
ZBRxIoSampleResponse ioSample;

File dataFile;

...

I am not really enlighted… Well with my .h file in libraries it works, but why is it mandatory? And (again) why these differences with Atmel?

The Arduino framework does some stuff with your code before feeding it
to the compiler - its usually a bad idea to define types in the sketch
because of this, put them in a .h (that's not a bad thing anyway).

Someone will have chapter/verse on exactly what goes on, but for instance
typedefs tend not to behave as you'd expect I believe.

The generated code is (seen in temporary folder) :

What does the generated code look like when the struct definition is in the sketch?

The prototype for the function that returns an Alert pointer is probably put before the struct definition that defines what an Alert is. So, the compiler then says "Hey, I have no idea what this is, so you can't do this".

MarkT:
The Arduino framework does some stuff with your code before feeding it
to the compiler

The polite description is "half-baked mucking about". You can probably guess what the non-polite description is. It's misguided and poorly implemented and unfortunately means that behaviour you'd reasonably expect of C++ doesn't occur in Arduino land.

What does the generated code look like when the struct definition is in the sketch?

I can't do some more tests right now, but if I understand well what is going on, my program "complexity" requires more efficient tools than just Arduino IDE...
So lesson learned, from now I will prefer Atmel that have a lot more optimization options and real C++ support (like a real IDE).

And BTW, I still think that Arduino IDE is a good start point for totally noob users like me! (C++ world is really complex)

Anyway, thank you all for your help and your enlightments!