Variable or field 'updateMessage' declared void, why?

Hi,
the code trying to print out the items in commands one by one and/or some one, got ERROR need help please.

Thanks
Adam


char ButtonA01, ButtonA02, ButtonB01, ButtonB02, ButtonC01, ButtonC02, ButtonD01;
char commands[] = {ButtonA01, ButtonA02, ButtonB01, ButtonB02, ButtonC01, ButtonC02, ButtonD01};
//// TOTAL 8 commands

int i, j;
void setup() {
  // put your setup code here, to run once:
  Serial.begin(9600);
}

void loop() {
  // put your main code here, to run repeatedly:
  i = 5;
  updateMessage(i);

  Serial.print(" updateMessage(i)=");
  Serial.println(updateMessage(i));

}

void updateMessage(i)
{
 for ( i = 0; i < 8; i++)

    dataToSend[8] = commands[i];

}

ERROR:

Arduino: 1.8.16 (Windows 7), Board: "Arduino Mega or Mega 2560, ATmega2560 (Mega 2560)"

sketch_mar22c:22:21: error: variable or field 'updateMessage' declared void

 void updateMessage(i)

                     ^

C:\Users\HUA.DELLV-PC\Documents\Arduino\sketch_mar22c\sketch_mar22c.ino: In function 'void loop()':

sketch_mar22c:15:3: error: 'updateMessage' was not declared in this scope

   updateMessage(i);

   ^~~~~~~~~~~~~

C:\Users\HUA.DELLV-PC\Documents\Arduino\sketch_mar22c\sketch_mar22c.ino: At global scope:

sketch_mar22c:22:21: error: variable or field 'updateMessage' declared void

 void updateMessage(i)

                     ^

exit status 1

variable or field 'updateMessage' declared void



This report would have more information with
"Show verbose output during compilation"
option enabled in File -> Preferences.

Try

int updateMessage( int i)

1 Like

The updateMessage function does not return anything. It is void.

Where is the dataToSend array declared. Will it have 9 elements (arrays are numbered from 0). So dataToSend[8] is the 9th element.

for ( i = 0; i < 8; i++)
    dataToSend[8] = commands[i];

What is the purpose for writing to element 8 over and over?

See the reference for arrays.

1 Like

Change:

void updateMessage(i)
{
...

to:

void updateMessage(int i)
{
...
1 Like

Thanks.
still can't print out say, commands[5] = ButtonC02.

When you make changes to the original code, post the new version, in a new post, so that we can keep up.

Post the new error if there is one.

1 Like

Thank you.


char ButtonA01, ButtonA02, ButtonB01, ButtonB02, ButtonC01, ButtonC02, ButtonD01;
char commands[] = {ButtonA01, ButtonA02, ButtonB01, ButtonB02, ButtonC01, ButtonC02, ButtonD01};
//// TOTAL 8 commands

char *dataToSend;

//int i, j;
void setup() {
  // put your setup code here, to run once:
  Serial.begin(9600);

  Serial.println(" setup!");

}

void loop() {
  // put your main code here, to run repeatedly:
  //  i = 5;
  updateMessage(5);

//  Serial.print(" updateMessage(5)=");
//  Serial.println(updateMessage(5));
  delay(1000);
}

//void updateMessage(i)

char updateMessage( int i)
{
   for ( i = 0; i < 8; i++)

  dataToSend = commands[i];

  Serial.print("dataToSend[9] = ");
  Serial.println(dataToSend);

}


Thanks.
What I like to do is Serial.print out the items one by one in commands[].

Now you've changed the updateMessage function signature so it is supposed to return a char, yet you do NOT return anything!

char updateMessage( int i)
{

It should be:

void updateMessage( int i)
{

The ONLY code inside your for loop is:

dataToSend = commands[i];
1 Like

Thanks.
I changed as:

void updateMessage( int i)
{

still can't print any thing.
when I say items in commands[], mean: ButtonA01, ButtonA02, ButtonB01, etc.

Where do you believe you are even attempting to print the commands? Your for loop does not print anything at all.

1 Like

The wokwi shown print some thing:


what I excepted is:
dataToSend[1] = ButtonA01;
dataToSend[2] = ButtonA02;
.....
how can get it?

Consider doing some research, C++ Functions - Return (w3schools.com)?

1 Like

First, these two lines:

char ButtonA01, ButtonA02, ButtonB01, ButtonB02, ButtonC01, ButtonC02, ButtonD01;
char commands[] = {ButtonA01, ButtonA02, ButtonB01, ButtonB02, ButtonC01, ButtonC02, ButtonD01};

probably do not do anything like what you expect.

The first line creates a bunch of named variables, but does NOT initialize their values, so they are ALL set to 0.

The second line creates the commands array which probably contains only one byte of the two (or more) byte addresses of each of those variables. In other words, it's useless.

So, what is it you are trying to do there, and what values do you expect to be in all those variables and the array?

As I've now pointed out twice, the updateMessage function contains a for loop, which accomplishes absolultely nothing. In fact, the for loop will be optimized away by the compiler, so it will not even exist in the compiled code.

If you are literally expecting to print out the names of the variables ButtonA01, etc., you won't be able to, because those names do not exist in the compiled code. All variable names are replaced with memory addresses in the compiled code, and the names are no longer present.

So, perhaps start by describing what it is you are actually TRYING to achieve.

Did you want something like this?!?

const char *commands[] =
{
  "ButtonA01", "ButtonA02",
  "ButtonB01", "ButtonB02",
  "ButtonC01", "ButtonC02", "ButtonD01"
};

//// TOTAL 7 commands

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

void loop()
{
  updateMessage(5);

  for (int i = 0; i < 7; i++)
  {
    Serial.print("updateMessage(i)=");
    updateMessage(i);
  }
}

void updateMessage(int i)
{
  Serial.print("commands[");
  Serial.print(i);
  Serial.print("] = ");
  Serial.println(commands[i]);
}

It repeatedly prints out:

commands[5] = ButtonC02
updateMessage(i)=commands[0] = ButtonA01
updateMessage(i)=commands[1] = ButtonA02
updateMessage(i)=commands[2] = ButtonB01
updateMessage(i)=commands[3] = ButtonB02
updateMessage(i)=commands[4] = ButtonC01
updateMessage(i)=commands[5] = ButtonC02
updateMessage(i)=commands[6] = ButtonD01
1 Like

Thank you johnwasser.
Tested works.

Thanks.
I was excepted declare a bunch of strings like the first line, not even ButionX, just any strings make them a group like the second line does, and be able to operate them.

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.