Hi, I'm creating a class BigInt which will be able to hold up to a 200 digit whole number, we want to be able to use it the same way you would use a regular int. Anyway my problem right now is when I try to use the set function (a member of BigInt class) in the string extraction operator (>>)
the errors I get are...
BigInt.cpp(53): error c2143: syntax error : missing ';' before '.'
when I try this
1 2 3
|
istream& operator >> (istream& whereFrom, string x ){
BigInt.set(x);
return whereFrom;}
| |
BigInt.cpp(53): error c2352: illegal call of non-static member function
when I try this
1 2 3
|
istream& operator >> (istream& whereFrom, string x ){
BigInt::set(x);
return whereFrom;}
| |
and it tells me "identifier 'set' not found, when I try this
1 2 3
|
istream& operator >> (istream& whereFrom, string x ){
set(x);
return whereFrom;}
| |
I found this in the archives, among many others, but they all seem to have been having issues with the string insertion operator not the extraction operator.
http://www.cplusplus.com/forum/beginner/550/
I'm using the set function because the set function takes in a string of 200 characters (including the '-' sign for negatives, thus negatives are only 199 digits long) and converts that into integers that can be used by the BigInt class (which uses an array of 50 indeces 4 digits long.)
Also the compiler I'm using is the visual c++ express edition compiler(Microsoft 32-bit C/C++ Optimizing Compiler Version 15.00.30729.01 for 80x86), I'm writing the actual code in notepad++
Anyway, I don't know how to call the member function any other way than those 3 examples, and I've been unable to find anything that has been helpful in telling me how I'm calling it wrong.
here's the header for the BigInt class:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30
|
/*Create a BigInt class
//data members:
//-array of 50 ints
//-bool for pos/neg
//functionality
//constructor
//operators: +,-,*,/, >>, <<, >, <, <=, >=, ==, !=, +=, -=, *=, /=, ++, --, %
*/
#include<iostream>
#include<string>
using std::string;
using std::ostream;
using std::istream;
class BigInt
{
friend ostream& operator << ( ostream&, const BigInt& );
friend istream& operator >> ( istream&, string );
public:
BigInt( string x );
//void print();
void set( string );
private:
int data[50]; //50 4 digit pieces
bool pos;
};
| |
and the code for the functions that I've done so far (Note: the set function was given to us by our instructor as we haven't gone over converting from one datatype to another)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75
|
// code for big int functionality
#include<iostream>
#include "BigInt.h"
#include<string>
using std::string;
using std::cout;
BigInt :: BigInt( string x ){
set(x);
}
/*void BigInt::print()
{
int i = 0;
while (data[i] == 0)
++i;
for (i; i < 50 ; ++i){
if (data[i] < 1000)
cout << "0";
if (data[i] < 100)
cout << "0";
if (data[i] < 10)
cout << "0";
cout << data[i];}
}*/
ostream& operator << (ostream& whereTo, const BigInt& x ){
int i = 0;
while (x.data[i] == 0)
++i;
for (i; i < 50 ; ++i){
if (x.data[i] < 1000)//if less than 1000, a 0 is inserted as a place holder for the 1000's place
whereTo << "0";
if (x.data[i] < 100)//if less than 100, a 0 is inserted as a place holder for the 100's place
whereTo << "0";
if (x.data[i] < 10)//if less than 10, a 0 is inserted as a place holder for the 10's place
whereTo << "0";
whereTo << x.data[i];}
return whereTo;}
/*istream& operator >> (istream& whereFrom, string x ){
BigInt::set(x); //this is what I'm currently having problems with
return whereFrom;}*/
void BigInt::set( string x ){
/* stopping point in the string, will be 0 if the number is positive, 1 if
there's a '-' on the front place will be used to put the digit in the
1s, 10s, 100s, or 1000s place bigintPos keeps track of where we are in the
value array (starting at the end and backing up) */
int stop = 0, place = 1, bigintPos = 49, startingPoint=x.length();
if ( x[0] == '-' ){
pos = false;
stop = 1;}
else
pos = true;
/* we can only hold a 200 digit number in our array, so if the string is too long,
we're only going to use the first 200 digits */
if ( x.length() - stop > 200 )
startingPoint = 200 + stop;
// fill up the value array with zeros initially
for ( int j = 0; j < 50; ++j )
data[j]=0;
for ( int i=startingPoint; i >= stop; --i ){
// only four digits per spot
if ( place == 10000 ){
place = 1;
bigintPos--;}
// we're going to ignore any non-numeric characters
if ( x[i] >= '0' && x[i] <= '9' ){
data[bigintPos] += place * (x[i]-'0');
place *= 10;}
}
}
| |