1

This code does not compile:

public class Calculadora<T> {
    T suma(T a, T b) {
        return (a + b);
    }
}

Calculadora<Integer> c = new Calculadora<Integer>();
System.out.println(c.suma(3, 5));

It gives me the error

The operator + is undefined for the argument type(s) T, T

How can I solve it?

1
  • This doesn't look like a duplicate of that question at all. The goal is addition, but the techniques attempted, and the attendant problems with them, are totally different. Commented Sep 24, 2014 at 8:03

3 Answers 3

4

T could be any class. It could even be an array. It's not ensured that the "+" operator will work for the given class. Why use generics for a calculator anyway?

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

Comments

1

Compiler does not know the type of T so + might not be valid on some cases.

But still if you wish you implement this with generics below can be one solution:

public class Calculadora<T> {

    Double suma(T a, T b) {
        if (a instanceof Number && b instanceof Number) {
            return (Double.parseDouble(a.toString()) +  Double.parseDouble(b.toString()));
        }
        return 0d;
    }

}

You must override toString method of T to return number.

1 Comment

0

Okay, the method you're trying to implement is impossible. Well, at least for me. I don't know how to solve it. But anyways, I'll try to explain why you're getting the error:

The operator + is undefined for the argument type(s) T, T

This simply means, What does a + do with a type T?

Well, it makes sense when we try to do something like:

Calculadora<Integer> c = new Calculadora<Integer>();

Because Integer can be cast to int and the compiler knows exactly how to add int with int right?

But since T can be of any type, what if we do something like:

Calculadora<Cat> c = new Calculadora<Cat>();

Clearly, we don't know how the hell you will add a Cat with another Cat. That's why the compiler is giving you that error, it's telling you, I don't know how to add this Type together. I can only add numbers and Strings.

Hope this'll help. :)

EDIT 2:

Well, the way I see it, if you really need to do something like that, you're going to have to change your class and method declaration a bit, something like:

//Limit only the Generics to subtypes of Number
class Calculadora<T extends Number> {

    //Return a specific type (Can be Double, Float, Long, etc.)
    Integer suma(T a, T b) {        
        //Change this based on your return type (Can be double, float, long, etc.)
        int sum = 0;
        if (a instanceof Number && b instanceof Number) {
            //Change this accordingly too (Can be doubleValue(), floatValue(), longValue(), etc.)
            sum = a.intValue() + b.intValue();
        }
        return sum;
    }
}

This is only for Integer. You can change this to any subtype of Number if you want.

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.