[英]error: void value not ignored as it ought to be in Arduino
這是一個 function 將 1 位數字轉換為 3 位數字。 例如將“2”轉換為“002”。
void loop() {
int x = convertdigit(time);
}
void convertdigit(int num){
char buffer[50];
int n;
n=sprintf (buffer, "%03d",num);
return buffer;
}
錯誤:應忽略的無效值
/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
我可以知道如何解決嗎?
當您編寫此代碼時,您是在告訴編譯器convertdigit
不返回任何內容(void):
void convertdigit(int num)
當您編寫此代碼時,您是在告訴編譯器使用convertdigit
的返回值並將其存儲在x
中:
int x = convertdigit(num);
這兩件事是沖突的:如果convertdigit
沒有返回任何東西,你怎么能在x
中存儲任何東西? 您的代碼有點令人困惑,所以我不確定您的實際意圖,但現在我已經解釋了該錯誤消息,我希望您能夠取得進展。
提示:如果您希望convertdigit
返回一個int
,請將void convertdigit(...
更改為int convertdigit(...
。
您應該將數字( 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
需要在 function 結束后保持變量可用(類似於全局變量):
n
在下一輪循環中保持其增加的值。
在converdigit()
返回后, buffer
可通過返回值獲得。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.