Class Issues

I can't seem to resolve this error:
big::bigint big::bigint::operator+(big::bigint&, big::bigint&)' must take either zero or one argument

I've googled a little but I don't really understand much about classes. I started them today. Here's the code segment with the problem:
Bignum.cpp:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
...
big::bigint big::bigint::operator + (big::bigint &num1, big::bigint &num2)
{
	big::bigint num3(num1);
	for (int i = 0; i < (num1.number.size() > num2.number.size() ? num1.number.size() : num2.number.size()); i++)
	{
		num3.number[i] += num2.number[i];
		if (i+1 == num3.number.size())
			num3.number.push_back(num3.number[i]/10);
		else
			num3.number[i+1] += num3.number[i]/10;
		num3.number[i] %= 10;
	}
	return num3;
}
...



Bignum.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
...
namespace big
{
	class bigint
	{
		public:
			bigint();
			bigint(bigint &);
			//~bigint();
	
			bigint &operator = (bigint &);
			bigint &operator = (std::vector <int> &);
			bigint operator + (bigint &, bigint &);//problem here with + operator overload.
			
			void disp_number() const;
		private:
			std::vector <int> number;
	};
...


Any other general advice is welcome.

P.S. On the bright side I have gotten input/output and assignment to work. :-)
Last edited on
closed account (3hM2Nwbp)
I'm not familiar with the "proper" terms to use describing what you need to do, but an overloaded operator+ member function should take a single argument. The "other" argument is the this pointer. To overload operator+ to take two arguments, the function should be moved outside of the class definition and be given the friend keyword.
closed account (D80DSL3A)
You only need 1 argument because the 1st operand (before the + sign) is the bigint calling the function (this). The argument supplied is the 2nd operand - the one after the + sign.
Last edited on
I've tried this but I get a different error in my BigTest.cpp file.
The error states : 14 no matching function for call to `big::bigint::bigint(big::bigint)'
13 candidates are: big::bigint::bigint(big::bigint&)

BigTest.cpp:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
#include<iostream>
#include"Bignum.h"

using namespace big;
using namespace std;

int main()
{
	bigint big1, big2;
	cout << "Enter a bigint.\n";
	cin >> big1;
	cout << "Enter a second bigint.\n";
	cin >> big2;
	cout << "Their sum is: " << big1+big2 << '\n';//error here
	cout << "Congratulations!!!";
	cin.get();
	return 0;
}

Bignum.h:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
...
namespace big
{
	class bigint
	{
		public:
			bigint();
			bigint(bigint &);
			//~bigint();
	
			bigint &operator = (bigint &);
			bigint &operator = (std::vector <int> &);
			bigint operator + (bigint &);
			
			void disp_number() const;
		private:
			std::vector <int> number;
	};
...

Bignum.cpp:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
...
big::bigint big::bigint::operator + (big::bigint &num)
{
	big::bigint num3(num);
	for (int i = 0; i < (num.number.size() > number.size() ? num.number.size() : number.size()); i++)
	{
		num3.number[i] += number[i];
		if (i+1 == num3.number.size())
			num3.number.push_back(num3.number[i]/10);
		else
			num3.number[i+1] += num3.number[i]/10;
		num3.number[i] %= 10;
	}
	return num3;
}
...
Last edited on
closed account (D80DSL3A)
Have you defined your copy constructor? The error reads like that may be the problem.
Your +operator function uses the copy constructor.
I just tested the copy constructor, it definitely works.
Your reference parameters should all be constant.
 
cin >> big1;


Do you actually have a ostream>> operator for bigint?
ostream>>


You mean ostream << operator, right?

Yes, I have successfully overloaded both insertion >>, and extraction << operators.


Your reference parameters should all be constant.


Hey, at first I didn't understand what you said, but I get it now, and it works. Now I just have to fix a small problem in my algo and then overload the other arithmetic operators.

Edit: Thanks for the help.
Last edited on
Topic archived. No new replies allowed.