Question
Why does the following program give me an declaration error? Aren't I declaring it at that specific line?
#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
Solution
You misspelled "BigInt" for "BitInt":
class BitInt
OTHER TIPS
The class is named "BitInt" when I presume it should be "BigInt". Just a typo.
This is your problem:
int main()
{
BigInt(); // <--- makes no sense
return 0;
}
It should be:
int main()
{
BigInt bigint; // create object of a class
return 0;
}
And you are declaring class BitInt and in main
using BigInt - there's typo one is Bit the other Big
On an unrelated note, defining MILLION as 1000000 is pointless. The reason to use named constants is to make the purpose of the number clear and allow you to change it easily, not just to let you type a number out in words instead of numerals.
It'd be better to call the constant BIGINT_DIGITS or something.
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow