0

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");
        }
    }
}
9
  • 1
    What error do you get? Commented Oct 7, 2015 at 17:19
  • 2
    a is not static and requires an instance. In a static method, you do not have an object instance. Commented Oct 7, 2015 at 17:20
  • The operator method is static. It requires 2 bigint parameters and returns a new instance: public static bigint operator +(bigint b1, bigint b2) Commented Oct 7, 2015 at 17:21
  • You are implementing the unary + operator, not the addition operator. There is no reference to instance fields (a) in static methods since a static method does not belong to any one instance of your bigint class. Commented Oct 7, 2015 at 17:21
  • Did you read the error message? Commented Oct 7, 2015 at 17:22

3 Answers 3

1

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

Sign up to request clarification or add additional context in comments.

3 Comments

Thank You.I tried with 2 arguments, but now I get another error
See the note I have in the middle about having to return a new instance of bigint. Don't modify the operands, store the result of the operation in a new bigint and return that. Thats what the public static bigint from the method template is, notice it doesn't say void, you have to return a new bigint.
Please mark as answer to get this off the unanswered questions list if your problem has been solved.
0

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.

Comments

0

Good Day,

The error that you are currently receiving is most probably the following error:

  • An object reference is required for the non-static field, method, or property 'bigint.a'

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:

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.