2013-05-29 23 views
-3

我發現了一個我想嘗試的begginers C++挑戰。但是,下面的代碼說我編譯它時包含錯誤。如果我試圖採取1號線的時間,它退出在結束第一類定義......我不知道什麼是錯的:)在C++中使用一個簡單的類,不工作?

#include <iostream> 
using namespace std; 

class Polynomial { 
    int a, b, c, functionValue; 

public: 
    Polynomial (int, int, int); 
    static void functionValue(Polynomial); 
}; 

Polynomial::Polynomial (int x, int y, int z) { 
    a = x; 
    b = y; 
    c = z; 
} 

void Polynomial::functionValue(Polynomial x) { 
    for (int i = 0; i < 5; i++) { 
     x.functionValue = x.a * pow(i, 2) + x.b * i + x.c; 
     cout << "The value of the function for x = " 
      << i << " is " << x.functionValue; 
    } 
} 

int main() { 
    Polynomial poly (2, 3, 5); 
    Polynomial::functionValue(poly); 

    system("pause"); 
    return 0; 
} 

我不知道爲什麼格式化這麼差。這裏是一個pastebin link.

(編輯:是我的錯,我編輯在以前的編輯和意外刪除這些 - BoBTFish)

編譯器錯誤:

'Polynomial::functionValue' : redefinition; previous definition was 'data member' 'see declaration of 'Polynomial::functionValue' 
'Polynomial::functionValue' : not a function' 'illegal reference to non-static member 'Polynomial::functionValue' 

在此先感謝。

+1

那麼編譯器錯誤是什麼? – BoBTFish

+0

對不起,會更新OP。 – wizH

+2

首先,您能否提供您正在獲取的_error_?查看錯誤是多麼容易。另請在錯誤信息的來源中註明。 –

回答

7

您有functionValue作爲變量,也作爲函數。

3

functionValue以兩種不同的方式使用兩次,一次作爲整數,另一次作爲靜態函數使用。

1

functionValue可以是任何成員或函數名稱。你應該重新命名這些。

還有什麼是使功能靜態的需要。

0

原因是還有一個名爲functionValue的變量。

我建議你安裝另一種C/C++編譯器 - Clang。 Clang給出了更多關於可能的錯誤的原因和位置的細節。

例如,我編寫了代碼Clang,它給了我以下信息。

test.cc:10:17: error: redefinition of 'functionValue' as different kind of symbol 
    static void functionValue(Polynomial); 
       ^
test.cc:6:18: note: previous definition is here 
    int a, b, c, functionValue; 
       ^
test.cc:19:18: error: redefinition of 'functionValue' as different kind of symbol 
void Polynomial::functionValue(Polynomial x) { 
       ^
test.cc:6:18: note: previous definition is here 
    int a, b, c, functionValue; 

這些給可能出現的錯誤的準確位置。

Clang對初學者很有幫助。

+0

將看看,謝謝:) – wizH

相關問題