2009-05-21 99 views
0

爲什麼下面的程序會給我一個聲明錯誤? 我不是在特定的行中聲明它嗎?C++類聲明

#include <iostream> 

#define MILLION 1000000 

using namespace std; 

class BitInt 

{ 
    public: 
    BigInt(); 

    private: 
    int digit_array[MILLION]; 
    int length; 
}; 

BigInt::BigInt() 
{ 
    int length=0; 
    for(int i=0; i<MILLION; i++) 
     digit_array[i]=0; 
} 

int main() 
{ 
    BigInt(); 

    return 0; 
} 

bigint.cpp:11: error: ISO C++ forbids declaration of ‘BigInt’ with no type 
bigint.cpp:18: error: ‘BigInt’ has not been declared 
bigint.cpp:18: error: ISO C++ forbids declaration of ‘BigInt’ with no type 
bigint.cpp: In function ‘int BigInt()’: 
bigint.cpp:22: error: ‘digit_array’ was not declared in this scope 

回答

3

你拼錯 「BigInt有」 爲 「BitInt」:

class BitInt 
+0

要創建一個實例,您需要使用「BigInt foo();」,而不是「BigInt();」主要。 – lothar 2009-05-21 03:11:40

0

類被命名爲 「BitInt」 時,我相信它應該是 「BigInt有」。只是一個錯字。

0

這是你的問題:

int main() 
{ 
    BigInt();  // <--- makes no sense 

    return 0; 
} 

它應該是:

int main() 
{ 
    BigInt bigint; // create object of a class 

    return 0; 
} 

而且您聲明類BitIntmain使用BigInt有 - 有錯字一個是畢ŧ其他Bi g

0

在一個不相關的說明中,將MILLION定義爲1000000毫無意義。使用命名常量的原因是爲了使數字的目的明確,並允許您輕鬆更改數字,而不僅僅是讓您用單詞而不是數字輸入數字。

最好是調用常量BIGINT_DIGITS什麼的。