expected constructor, destructor, or type conversion before....

Hi
This is my code:

#include <stdio.h>
#include <string.h>
#include <ESP8266WiFi.h>

char myTopicPrefix[20];
char clientID[20];
strcpy(myTopicPrefix,"CGTK");
strcat(myTopicPrefix, ((String)ESP.getChipId()).c_str());
strcpy(clientID, "CGUID");
strcat(clientID, ((String)ESP.getChipId()).c_str());

void setup() { 
}


void loop() {
  
}

And this is the error message I get:

error: expected constructor, destructor, or type conversion before '(' token

The first lines are out of the setup() on purpose and need to be before it.

What am I doing wrong?

thanks…

Have you tried commenting out all but one line to see what the machine is upset about?

-jim lee

This:

strcpy(myTopicPrefix,"CGTK");
strcat(myTopicPrefix, ((String)ESP.getChipId()).c_str());
strcpy(clientID, "CGUID");
strcat(clientID, ((String)ESP.getChipId()).c_str());

it must be in function body.

Budvar10:
This:

strcpy(myTopicPrefix,"CGTK");

strcat(myTopicPrefix, ((String)ESP.getChipId()).c_str());
strcpy(clientID, “CGUID”);
strcat(clientID, ((String)ESP.getChipId()).c_str());



it must be in function body.

I tried to put it in function buddy:

#include <stdio.h>
#include <string.h>
#include <ESP8266WiFi.h>

char myTopicPrefix[20];
char clientID[20];
char myTopicPrefix[20];
char clientID[20];
void beforeSetup(){
    strcpy(myTopicPrefix,"CGTK");
    strcat(myTopicPrefix, ((String)ESP.getChipId()).c_str());
    strcpy(clientID, "CGUID");
    strcat(clientID, ((String)ESP.getChipId()).c_str());
}
beforeSetup();

void setup() { 
}


void loop() {
  
}

but I keep getting this error:

error: expected constructor, destructor, or type conversion before ';' token

 beforeSetup();

The code that you posted has several errors, staring with multiple declarations of variables

char myTopicPrefix[20];
char clientID[20];
char myTopicPrefix[20];
char clientID[20];

and the fact that ESP is not declared at all

and the call to beforeSetup(); is still not in a function.

Have you posted the correct code ?

UKHeliBob:
The code that you posted has several errors, staring with multiple declarations of variables

char myTopicPrefix[20];

char clientID[20];
char myTopicPrefix[20];
char clientID[20];



and the fact that ESP is not declared at all

and the call to beforeSetup(); is still not in a function.

Have you posted the correct code ?

Hi you right I posted by mistake I fixed the code:

#include <stdio.h>
#include <string.h>
#include <ESP8266WiFi.h>

char myTopicPrefix[20];
char clientID[20];
void beforeSetup(){
    strcpy(myTopicPrefix,"CGTK");
    strcat(myTopicPrefix, ((String)ESP.getChipId()).c_str());
    strcpy(clientID, "CGUID");
    strcat(clientID, ((String)ESP.getChipId()).c_str());
}
beforeSetup();

void setup() { 
}


void loop() {
  
}

And I think that

UKHeliBob:
and the call to beforeSetup(); is still not in a function.

Is the reason that the code fail to compile. Why do I need to call beforeSetup() inside a function.what if I need to do this before setup()?

If you want to call the function in C, it must be from another function body. It is C syntax. On the top level is the function main(). C program must contain it, but Arduino is also for noobs and the main() is hidden. There are setup() and loop() instead. Everything you must write into their body. Setup() is executed once after resetting the Arduino and loop() is executed after setup again and again, till the Arduino will be powered.

what if I need to do this before setup()?

Call it as the first thing in setup()