Reputation: 63
It gives an error, when I write a.Length in operator+.
Why?
class bigint
{
private static int[] a3;
private string a;
public bigint(string a)
{
this.a = a;
}
public static bigint operator+(bigint b)
{
int max, min, q;
if (a.Length > b.a.Length)
{
max = a.Length; min = b.a.Length;
q = max - min;
for (int j = 0; j < q; j++)
{
b.a = b.a.Insert(0, "0");
}
}
else
{
max = b.a.Length; min = a.Length;
q = max - min;
for (int j = 0; j < q; j++)
{
a = a.Insert(0, "0");
}
}
}
Upvotes: 0
Views: 344
Reputation: 11273
Unfortunately you have a few answers, but none really give the code you need to fix your problem.
There are two +
operators in C# (just like 2 -
operators, although the unary +
is much less useful). The unary version uses a single argument to explicitly identify the right side of the equation as positive, just like you would with a negative number (i.e. -10).
To fix your problem, you need to create a binary operator:
public static bigint operator+(bigint lhs, bigint rhs)
{
//Code here
}
The reason you are getting the error that you are is that operator
overloads must be defined as static methods. Static methods are not tied to any instance of the class that they are defined in, so when you say a.Length
it doesn't have any idea what a
is (it does, but it realizes you are trying to use an instance variable/property from a static method).
Also realize that you have to return a new instance of bigint
that contains the result of the operation, you should not ever modify the operands directly! Why?
Think of it like this, if you have:
bigint a = new bigint();
bigint b = new bigint();
bigint c = a + b;
The way you have it written, the result will either store in a
or b
, but nothing in c
. That means that you've modified a
and/or b
unpredictably.
Think of static methods like extensions to your class, they can't talk to anything that is an instance (not declared with static
). It wouldn't make sense to make a
static since it has to contain different values for different instances of bigint
.
So the fix here is to simply implement the binary +
operator correctly. Usually I name them lhs
and rhs
for "left hand side" and "right hand side" respectively so I know which operand is on which side (which is important for some operations like division).
Upvotes: 1
Reputation: 1
Good Day,
The error that you are currently receiving is most probably the following error:
The problem is your private variable that you created called "a" is not of the static type and because you want to use your "a" variable in the overloaded + operator method which is marked as static you need to also mark your variable as a static variable in order to use it in the static method.
But your entire structure of how you want to overload the + operator method for the "bigint" class is incorrect.
Please see the following article from MSDN to know how to properly overload a operator method:
Upvotes: 0
Reputation: 4104
Your method is static
, which means it can be used without an instance of your bigint
class. Since string a
is a property of the class, you need an instance for it to have a value.
static
Methods cannot access non-static members of the class.
Upvotes: 0