2013-12-17 36 views
0

我在C++寫一個轉換器形式的意大利lires爲歐元:錯誤:預計','或';'前「{」令牌「和」不能被用作函數

#include <iostream> 

using namespace std; 

float x; 

int converter (x) 
{ 
    y = x/1936,27; 
    return y; 
} 

int main() 
{ 
    cout << "Give me the value: "; 
    cin >> x; 
    converter (x); 
} 

我嘗試編譯它,我看到了兩個錯誤。第一個是:

lire-euro.cpp:8: error: expected ‘,’ or ‘;’ before ‘{’ token 

我在括號之前定義了一個函數。爲什麼我應該在'{'之前放','或​​3210? 第二個是:

lire-euro.cpp: In function ‘int main()’: 
lire-euro.cpp:17: error: ‘converter’ cannot be used as a function 

爲什麼我不能用converter的功能?這與其他錯誤有關嗎?

回答

3

你的函數參數列表中缺少參數類型:

int converter (float x) { ... 
//    ^^^^^ 

除此之外,您使用的y功能,這是未申報的體內。您可以通過返回表達式來解決該問題,但您可能必須將浮點字面上的,替換爲.,具體取決於您的語言環境。

return x/1936.27; 

注意,這可能更有意義返回一個浮點數,而不是一個int

最後,我認爲沒有理由讓x成爲全球性的。你可以聲明它在main()之內:

#include <iostream> 

int converter(float x) 
{ 
    return x/1936.27; 
} 

int main() 
{ 
    float x; 
    std::cout << "Give me the value: "; 
    std::cin >> x; 
    int z = converter(x); 
} 
1

你在函數定義中有兩個錯誤。首先它的參數x沒有類型說明符,它的局部變量y也沒有被定義。

我認爲你的意思

float x; 

int converter() 
{ 
    int y = x/1936,27; 
    return y; 
} 

雖然我不知道Y(和函數的返回類型)是否應該被定義爲int。

的functioo的相應的呼叫可能看起來如

cout << converter() << endl; 
+0

第三,雖然不是錯誤時,'converter'可以改寫成'X/1936; int y = 27;'爲了清楚起見。 :) – mars

+0

@mars,你是對的。我沒有看到有逗號。 –

相關問題