2009-05-21 105 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什么的。