Reputation: 13
I'm trying to do a sum but i always get that type error .The class OperateurBinair takes two parameters of type Constante , Constante is a class in which i create values (type = double ).
public class Plus extends OperateurBinair {
Constante Left;
Constante Right ;
Constante Somme ;
Plus() {
super();
Left = new Constante();
Right = new Constante();
}
Plus(Constante x , Constante y) {
super(x,y);
Left=x;
Right=y;
}
Constante addition() {
return Left + Right;
}
}
Upvotes: 0
Views: 157
Reputation: 17226
Constante
is an object and you cannot add objects (with the exception of String
, which is a special case), because the meaning of that would change from object to object, if it had a meaning at all.
Constante
may have a method constante.add(otherConstante)
or if it's your own class you could write one. the .add()
method would most likely add whatever numberic values are contained within Constante
.
Alternatively, stop using the class Constante
and start using a double (or other numeric primitive) if you do not require object behaviour.
Variable names like Left
should not start with a capital letter by convention. Classes are in UpperCamelCase, variables are in lowerCamelCase.
Upvotes: 0
Reputation: 62062
Replace
return Left + Right;
with
return Constante(Left.getter() + Right.getter());
Where getter
is the getter
method for whatever value you'r actually trying to sum. This also assumes you have a constructor for Constante
that takes a double
argument. If not, you'll need something more like this:
Constante sum = new Constante;
sum.setter(Left.getter() + Right.getter());
return sum;
(Or just add a constructor that takes a double.)
Or alternatively, you can add a method to Constante
to do the summing.
public static Constante sum(Constante addend1, Constante addend2) {
//do whatever logic you want for summing these and return a Constante
//with the new value
}
Then in the class you're currently working in, you can do:
return Constante.sum(Left, Right);
Upvotes: 3