[英]error: void value not ignored as it ought to be in Arduino
This is a function to convert 1 digit number to 3 digit.这是一个 function 将 1 位数字转换为 3 位数字。 For example convert '2' to '002'.例如将“2”转换为“002”。
void loop() {
int x = convertdigit(time);
}
void convertdigit(int num){
char buffer[50];
int n;
n=sprintf (buffer, "%03d",num);
return buffer;
}
Error: void value not ignored as it ought to be错误:应忽略的无效值
/sketch/sketch.ino: In function 'void loop()':
/sketch/sketch.ino:33:30: error: void value not ignored as it ought to be
int x = convertdigit(time);
^
Error during build: exit status 1
May i know how to fix it?我可以知道如何解决吗?
When you write this you are telling the compiler that convertdigit
does not return anything (void):当您编写此代码时,您是在告诉编译器convertdigit
不返回任何内容(void):
void convertdigit(int num)
When you write this, you are telling the compiler to use the return value of convertdigit
and store it in x
:当您编写此代码时,您是在告诉编译器使用convertdigit
的返回值并将其存储在x
中:
int x = convertdigit(num);
Those two things are in conflict: if convertdigit
doesn't return anything, how can you store that nothing in x
?这两件事是冲突的:如果convertdigit
没有返回任何东西,你怎么能在x
中存储任何东西? Your code is kind of confusing so I'm not sure what you actually intended, but now that I have explained that error message, I hope you are able to make progress.您的代码有点令人困惑,所以我不确定您的实际意图,但现在我已经解释了该错误消息,我希望您能够取得进展。
Hint: If you want convertdigit
to return an int
, change void convertdigit(...
to int convertdigit(...
.提示:如果您希望convertdigit
返回一个int
,请将void convertdigit(...
更改为int convertdigit(...
。
You should distinguish numbers ( 2 ) from texts "002"您应该将数字( 2 )与文本“002”区分开来
void loop() {
static byte n = 1;
char* txt = convertdigit(n); // Convert to a 2 character text with a leading zero, if necessary;
Serial.println(txt);
delay(100);
n++;
if (n > 99) n=0;
}
char* convertdigit(byte num) {
static char buffer[4];
sprintf (buffer, "%02d", num);
return buffer;
}
static
is required to keep the variable available after function has ended (similar to global variables): static
需要在 function 结束后保持变量可用(类似于全局变量):
n
keeps its incremented value in the next loop round. n
在下一轮循环中保持其增加的值。
buffer
is available via return value after converdigit()
returns.在converdigit()
返回后, buffer
可通过返回值获得。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.