2011-09-23 42 views
9

我在編譯C代碼時遇到了問題。 當我編譯,I'l得到這個錯誤:GCC C編譯錯誤,void值不會被忽略,因爲它應該是

player.c: In function ‘login’: 
player.c:54:17: error: void value not ignored as it ought to be 

這是錯誤的代碼:

static bool login(const char *username, const char *password) { 
    sp_error err = sp_session_login(g_sess, username, password, remember_me); 
    printf("Signing in...\n"); 
    if (SP_ERROR_OK != err) { 
     printf("Could not signin\n"); 
     return 0; 
    } 
    return 1; 
} 

任何繞過這種錯誤的方式?
感謝

編輯: 以sp_所有功能都從libspotify

+3

顯示sp_session_login的'的聲明()'。 –

+0

你有在C中定義的布爾?不好的做法。 – Cartesius00

+0

如果你使用'clang',你也可能得到更好的錯誤信息。 –

回答

8

錯誤線在哪裏?

沒有進一步的信息,我猜它在這裏:

sp_error err = sp_session_login(g_sess, username, password, remember_me); 

我猜sp_session_login將退回作廢。

嘗試:

static bool login(const char *username, const char *password) { 
    sp_session_login(g_sess, username, password, remember_me); 
    printf("Signing in...\n"); 
    return 1; 
} 
+2

確認,'sp_session_login'是一個'void'函數:https://developer.spotify.com/docs/libspotify/11.1.60/group__session.html –

8

它通常意味着你分配一個空函數的東西回報,這當然是一個錯誤。

在你的情況,我猜sp_session_login函數是無效的,因此錯誤。

2

我猜測sp_session_login被宣佈爲返回void而不是sp_error並且有一些確定其是否成功的替代方法。

2

它看起來不像sp_session_login實際上返回任何東西。特別是,它不會返回sp_error,所以這是行不通的。你不能真的繞過它。

-1

您必須聲明void函數之前使用它們。嘗試將它們放在主函數之前或者在他們的調用之前。 還有一件事你可以做:你可以告訴編譯器你將使用void函數。

對於exemplo,有兩種方法可以讓同樣的事情:

#include <stdio.h> 

void showMsg(msg){ 
    printf("%s", msg); 
} 

int main(){ 
    showMsg("Learn c is easy!!!"); 
    return 0; 
} 

......另一路:

#include <stdio.h> 

void showMsg(msg); //Here, you told the compiller that you will use the void function showMsg. 

int main(){ 
    showMsg("Learn c is easy!!!"); 
    return 0; 
} 

void showMsg(msg){ 
    printf("%s", msg); 
} 
+2

這並不回答問題。 *所有*功能應在使用前聲明(C99要求這樣做,這在C90中是很好的做法)。OP已經通過'#include'標識了'sp_session_login';這就是編譯器知道它是一個「void」函數的方式。問題在於OP試圖將不存在的'void'函數的結果賦值給一個變量;你的回答沒有解決實際問題。 –

相關問題