views:

141

answers:

6

For learning purposes I'm creating big integer class in C++. There are 2 files:

big_int.h

#ifndef BIG_INT_H
#define BIG_INT_H

#include 

class big_int
{
public:
    big_int(void);
    big_int(char*);
    big_int(QString);

    ~big_int();

    big_int operator+(big_int);
    big_int operator-(big_int);
    big_int operator*(big_int);
    big_int operator/(big_int);
};

#endif // BIG_INT_H


big_int.cpp


#include "big_int.h"

big_int::big_int()
{
}

big_int::big_int(QString str)
{
}

big_int::~big_int()
{
}

big_int operator+(big_int b)
{
    return big_int();
}

big_int operator-(big_int b)
{
    return big_int();
}

big_int operator*(big_int b)
{
    return big_int();
}

big_int operator/(big_int)
{
    return big_int();
}

Qt Creator returns: C:/Documents and Settings/Admin/My Documents/calculator_1_0/big_int.cpp:31: error: big_int operator/(big_int) must take exactly two arguments. But operator/ takes only 1 parameter. What`s wrong?

+2  A: 

That's a typo, you forgot the class name :

big_int big_int::operator+(big_int b)
{
    return big_int();
}

big_int big_int::operator-(big_int b)
{
    return big_int();
}

big_int big_int::operator*(big_int b)
{
    return big_int();
}

big_int big_int::operator/(big_int)
{
    return big_int();
}

By the way, you should take contant references instead of values :

big_int big_int::operator/(const big_int& v)
{
    //...
}
Samuel_xL
A: 

You need to specify the class for each of the functions.

So change:

big_int operator+(big_int b)

to:

big_int big_int::operator+(big_int b)

Otherwise you need to specify 2 parameters for operators that work on 2 values like +, *, /, ...

But since you put the declaration of all the operators inside your class I think you want my first suggestion.

Brian R. Bondy
A: 

You forgot to prepend the operator methods definitions with the class name qualifier. They should look like this:

bigint bigint::operator/(big_int)
{ //method body }

BTW, the arguments to your operators should be passed as references, not values for efficiency reasons.

Altariste
+3  A: 

specify the class name as suggested by @TheSamFrom1984.

OR Provide operators which takes two parameters for big_int.

class big_int
{
public:
    big_int(void);
    big_int(char*);
    big_int(std::string);

    ~big_int();

    friend big_int operator+(big_int, big_int);
    friend big_int operator-(big_int, big_int);
    friend big_int operator*(big_int, big_int);
    friend big_int operator/(big_int, big_int);
};
aJ
+1  A: 

It is usually better to declare operators taking two operands as free functions outside the class and to declare inside the class the corresponding X= version. For instance:

class big_int
{
public:
  big_int& operator += (const big_int& rhs);
};

big_int operator + (const big_int& lhs, const big_int& rhs);

big_int& big_int::operator += (const big_int& rhs)
{
    ...
    return *this;
}

big_int operator + (const big_int& lsh, const big_int& rhs)
{
    big_int rc(lhs);
    rc += rhs;
    return rc;
}
Johan
best practice indeed. Btw you can pass lsh by value, this way you don't have to copy it.
Samuel_xL
A: 

There are already enough explanations here on how to fix this, but as a side note, it's good to understand why you got a compiler error only for the / operator. The first three operators +, - and * are also unary operators that only take one argument, for example:

int x = 3;
int y = +x; // unary + operator
int z = -x; // unary - operator
int *p = &x;
int q = *p; // unary * operator

So even though the compiler didn't complain for those operators, it follows that even they would not work as you probably intended. So be careful ;)

catchmeifyoutry