0

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?

1
  • A few more hints on doing it the right way: Define operator+= first, and define operator+ in terms of that (create a copy of one addend and add the other to it). You are going to want a copy constructor (big_int::big_int(const &big_int)). Less importantly, you should normally accept and return const references (const big_int & big_int::operator+=(const &big_int), although that's more a performance thing, so you don't need to worry about that yet. Commented Dec 2, 2009 at 16:17

6 Answers 6

5

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); }; 
Sign up to request clarification or add additional context in comments.

Comments

3

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) { //... } 

Comments

1

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; } 

1 Comment

best practice indeed. Btw you can pass lsh by value, this way you don't have to copy it.
0

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.

Comments

0

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.

Comments

0

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 ;)

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.