HugeInt class c++

i need to make a class that will allow the adding, subtracting, division, multiplication, as well as greater than, less than, equal to, >=, <=, ==, !=, and =zero functions.

my code's attatched, i am getting this error with my division function:
HugeInt.cpp:214: error: no match for ‘operator+’ in ‘answer + 1’

this has been laughing in my face for the last 12 hours. if anybody can help please and thank you.
sold22Asked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

sold22Author Commented:

//HugeIntiger.hy
//Created 4-5-10
// header file to prevent multiple inclusions
#include <iostream>
using namespace std;

#ifndef _HUGEINTIGER_H
#define	_HUGEINTIGER_H

// HugeInt class definition

int maxsize = 40;


class HugeInt
{
    public:
        HugeInt( void );
        ~HugeInt( void );

        HugeInt( long long size, int digits[40]);
        HugeInt( const HugeInt &h);

        friend ostream &operator>>( ostream &output, const HugeInt &outvalue);
        friend istream &operator<<( istream &input, const HugeInt &invalue);

        HugeInt operator =( HugeInt rhs) ;
        HugeInt operator +(  HugeInt& rhs);
        HugeInt operator -( const HugeInt& rhs) const;
        
        HugeInt operator *( HugeInt rhs) ;
        HugeInt operator /(  HugeInt& rhs) ;
        HugeInt operator %( const HugeInt& rhs) const;

        bool operator <(const HugeInt& rhs) const;
        bool operator <=(const HugeInt& rhs) const;

        bool operator >(const HugeInt& rhs) const;
        bool operator >=(const HugeInt& rhs) const;

        bool operator ==(const HugeInt& rhs) const;
        bool operator !=(const HugeInt& rhs) const;
        bool operator !() const;
        
        size_t getSize( );
        void setSize( size_t size );
        void sepDigits ( size_t size, int getDigits[40] );
        long long getnumbr();

    private:
        int digits[40];
        size_t size;
        long long numbr;

}; // end class HugeInt

#endif	/* _HUGEINTIGER_H */




//HugeInt.cpp


#include "HugeIntiger.h"

#include <iostream>
#include <iomanip>
#include <cmath>

using namespace std;

HugeInt::HugeInt( void )
{
    size = 0;
    numbr = 0;

    for(int x=0; x<40; x++)
        digits[x]=0;

}

HugeInt::~HugeInt( void )
{
}


HugeInt::HugeInt( long long init, int digits[40] )
{
    // initialize private data members to zero
   size = 0;
   numbr = init;
   
   // initialize the array to zero
   for (int x = 0; x<40; x++)
       digits[x] = 0;

   int setSize();
   int setdigits();


}

HugeInt::HugeInt( const HugeInt &h)
{
    size = h.size;
    numbr = h.numbr;

    for( int x = 0; x<40; x++)
        digits[x] = h.digits[x];
}

// gets size of array
size_t HugeInt::getSize( )
{

    for(int x = 39; x >= 0; x--)
    {
        if(digits[x] == 0)
             maxsize--;
        else
            return maxsize;
    }
    return 0;
}

// sets size
void HugeInt::setSize( size_t size)
{
    size = 0;
    long long num = numbr;

    for(long long temp = 10; num > 0; size++)
        num /= temp;
}

// get intiger. this is used for subtraction
long long HugeInt::getnumbr()
{
    return numbr;
}

// seperates Number
void HugeInt::sepDigits( size_t size, int digits[40])
{
    long long n = numbr;
    size_t power = 1;
    int temp =0;
    for(size_t x = size - 1; x >= 0; x--)
    {
        for( size_t y = x; y > 0; y--)
            power*= 10;

        temp = (n/power);
        digits[x] = temp;
        n-= digits[x]*power;
        power = 1;
    }
}

// equals function definition
HugeInt HugeInt::operator =(  HugeInt rhs) 
{
    for(int x = 0; x<40; x++)
        digits[x] = rhs.digits[x];

    return rhs;

}

//addition member function definitions
HugeInt HugeInt::operator +( HugeInt& rhs)
{
    HugeInt answere;
    HugeInt lhs = *this;
    HugeInt rhscopy(rhs);
    int x = 0;

    //set greater size in order to add
    if (lhs > rhs)
        x = lhs.getSize();
    else
        x = rhscopy.getSize();

    // addition
    for( int y = 0; y <= x; y++)
    {
        answere.digits[y] = digits[y] + answere.digits[y];

        if ( answere.digits[y] > 9)
        {
            answere.digits[y+1] = answere.digits[y]/10;
            answere.digits[y]%= 10;
        }
    }
    return answere;

}

//subtraction function definitions
HugeInt HugeInt:: operator -(const HugeInt& rhs) const
{
    HugeInt answere;
    HugeInt lhs = *this;
    int x = 0;

    // check to make sure the proper numbers are used. can't return negetive values
    if (lhs < rhs)
    {
        answere.numbr = -1;
        return answere;
    }

    //subtration
    for (x = lhs.getSize(); x>= 0; x--)
    {
        if( digits[x] < rhs. digits[x])
        {
            answere.digits[x] =  10 + digits[x] - rhs.digits[x];
            answere.digits[x+1]--;
        }
        else
            answere.digits[x] = digits[x] - rhs.digits[x];
    }
    return answere;

}

// multiplication function definitions
HugeInt HugeInt::operator *( HugeInt rhs) 
{
  
    HugeInt answere;
    HugeInt lhs = *this;
    HugeInt rhscopy = rhs;

    size_t temp = rhscopy.getSize( );
    int x;
    int y;
    int power = 1;
    

    if (lhs > rhs)
    {
        for( x=0; x<=temp; x++)
        {
            for(y=0; y< rhs.digits[x]*power; y++)
                answere = answere + rhs ;
            y=0;
            power*=10;
        }
    }
    else
    {
        for(x=0;x<=lhs.getSize(); x++)
        {
            for(y=0; y<digits[x]*power; y++)
                answere = answere + rhs ;
            y=0;
            power*=10;
        }
    }
        return answere;
}

// division function definitions
HugeInt HugeInt::operator /( HugeInt& rhs)
{

    HugeInt lhs = *this;
    HugeInt answer ;
    HugeInt a = lhs;
    HugeInt b = rhs;

    for ( a; a>b; answer = answer + 1)
        a= a-b;
    if (a==b)
        answer = answer+1;

    return answer;
}

// less than function definitions
bool HugeInt::operator <(const HugeInt& rhs) const
{
    for(int x=39; x>=0; x--)
    {
        if(rhs.digits[x] > digits[x])
            return true;
        if(rhs.digits[x] < digits[x])
            return false;
    }
    return false;
}

// less than or equal to function definitions
bool HugeInt::operator <=(const HugeInt& rhs) const
{
    HugeInt lhs(*this);

    if(lhs == rhs || lhs<rhs)
        return true;
    else
        return false;
}

// greater than function definitions
bool HugeInt::operator >(const HugeInt& rhs) const
{
    for(int x=39; x>=; x--)
    {
     if(rhs.digits[x] < digits[x])
         return true;
     if(rhs.digits[x] < digits[x])
         return false;
    }
    return false;
}

//greater than or equal to function definitions
bool HugeInt::operator >=( const HugeInt& rhs) const
{
    HugeInt lhs(*this);
    if(lhs==rhs || lhs>rhs)
        return true;
    else
        return false;
}

// equal to function definitions
bool HugeInt:: operator ==(const HugeInt& rhs) const
{
   for(int x=39;x>=0; x--)
   {
       if(rhs.digits[x] < digits[x])
           return false;
       if(rhs.digits[x] < digits[x])
           return false;
   }
   return true;
}

// not equal to function definitions
bool HugeInt::operator !=(const HugeInt& rhs) const
{
    HugeInt lhs(*this);

    if(lhs == rhs)
        return false;
    else
        return true;
}

// check to see if = to 0 function
bool HugeInt::operator !() const
{
    HugeInt lhs(*this);

if(lhs == 0)
    return true;
else
    return false;
}

// ostream& operator function definitions
ostream& operator <<(ostream &output, HugeInt &outValue)
{
    for(int i = outValue.getSize() -1; i>=0; i--)
        output << outValue.digits[i];

    if(outValue ==0)
        output<<"0";

    return output;
}

istream& operator >>(istream &input, HugeInt &inValue)
{
    char tm[40];
    int i = 0;
    int j = 0;

    input >> tm;

    while(tm[i] !=0)
        i++;
    for(; i>0; i--)
    {
        inValue.digits[j] = [i-1] -'0';
        j++;
    }
    return input;
}

Open in new window

0
Infinity08Commented:
>> HugeInt.cpp:214: error: no match for ‘operator+’ in ‘answer + 1’

You are trying to add an int to a HugeInt, but you have not implemented an operator+ that can do that :

        HugeInt operator+(int rhs);
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Infinity08Commented:
Or better yet - make that const :

        HugeInt operator+(int rhs) const;
0
Bootstrap 4: Exploring New Features

Learn how to use and navigate the new features included in Bootstrap 4, the most popular HTML, CSS, and JavaScript framework for developing responsive, mobile-first websites.

sold22Author Commented:
my code's really jumbled. the test main function will only add two objects of type hugeint together.
do i still need an operator for that?
0
Infinity08Commented:
>>         answer = answer+1;

Here, you're trying to add an int to a HugeInt. If you want to be able to do that, you'll need to add that operator, yes.
0
phoffricCommented:
Alternatively, you could define a constructor that accepts an int as an argument.
And then you could say
    answer = answer + HugeInt(1);
0
cupCommented:
I would have thought it should be

 HugeInt operator+(const int rhs) const;

0
Infinity08Commented:
The const for the int parameter doesn't really matter, does it ? ;) In fact, it limits you a bit more than necessary.
0
cupCommented:
What I've found, from playing with several compilers is that const int allows both constants and variables but int only allows variables.  So if you have

HugeInt x (0)
int y = 1;
x = x + y;    // ok because y is int
x = x + 1;    // not ok because 1 is const int

On some compilers it will work if it is just int but on others you will get an error.  To get round the problem  make it a const int.
0
Infinity08Commented:
>> that const int allows both constants and variables but int only allows variables

The int is passed in by value, so it should work with either. If it doesn't, then the compiler is broken.
0
cupCommented:
Oh well, I stand corrected.  Just that on certain circumstances where this sort of error has come up (something to do with not being able to find a matching routine), it wants a const.
0
Infinity08Commented:
When passing by reference or by pointer, that is certainly true, and you have to make it const.
0
itsmeandnobodyelseCommented:
>>>> Alternatively, you could define a constructor that accepts an int as an argument.
>>>> And then you could say
>>>>     answer = answer + HugeInt(1);

If you provided the constructor HugeInt::HugeInt(int)

    answer = answer + 1;

should work also. The compiler would find the appropriate operator+ in that case and would automatically convert the 1 to HugeInt.
0
phoffricCommented:
answer = answer + 1; // did not compile with just adding HugeInt::HugeInt(int)
But answer = 1 should compile with just that constructor added.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Editors IDEs

From novice to tech pro — start learning today.