I have search though multiple similar topics but still can't find the answere. Would you please help ?
The error is :
invalid initialization of reference of type 'ArrayT<float>&' from expression of type 'const Arrat<float>'
The above errors occur when I tried to do the following code , with operator* overloading :
1 2 3 4 5 6
const ArrayT<float>& b1 = A*A;
ArrayT<float>& c2 = b1*A;// <---- this line makes the error
//b1 is a const ref of ArrayT<float>
//A is just a normal object of ArrayT<float> created by ArrayT<float> A(blah,blah,blah);
The following are the list of operator* overloading :
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
template <class T>
ArrayT<T>& ArrayT<T>::operator*(ArrayT<T>& b) {blah,blah,blah}
template <class T>
const ArrayT<T>& ArrayT<T>::operator*(ArrayT<T>& b) consttemplate <class T>
const ArrayT<T>& ArrayT<T>::operator*(const ArrayT<T>& b) consttemplate <class T>
ArrayT<T>& ArrayT<T>::operator*(const ArrayT<T>& b)
template <class S>
ArrayT<S>& operator*(const ArrayT<S>& a,ArrayT<S>& b) //(***)
// <--------This (***) one I want to use for error multiplication above, but not success.
> invalid initialization of reference of type 'ArrayT<float>&' from expression of type 'const Arrat<float>'
Alice sees an object. To Alice, that object is unalterable.
Bob ask Alice where is that object, then breaks it with a hammer.
> But that's my requirement
¿what is your requirement?
You shouldn't need so many prototypes Array<T> Array<T>::operator*(const Array<T>&) const should be enough.
It's quite weird that you are returning a reference where one would be expecting a temporary, ¿are you changing the semantic?
That style using here is not mine. It's from one of the instructor at my school who made it. He gave a list of similar style of operator+ overloading, then he made us to do the operator*. It's a some what big code with almost a thousand lines, so I can not put all in here.
By that way what is Alice and Bob here. in the ArrayT<float>& c2 = b1*A , from what I understand, b1*A , b1 is a reference to a const object ArrayT<float>,it should use the ArrayT<T>& ArrayT<T>::operator*(ArrayT<T>& b) , since A is not const. b1*A= b1.add(A).
That style using here is not mine. It's from one of the instructor at my school who made it.
If you're not misinterpreting it, your instructor doesn't have a clue what he's talking about, but I would guess you're misinterpreting things.
I would peruse the source he gave you for the operator+ overloading. I'm almost certain you'll find some of those aren't operator+, but operator+=, and that not all of the return types are references.
> from what I understand, b1*A , b1 is a reference to a const object ArrayT<float>,
> it should use the ArrayT<T>& ArrayT<T>::operator*(ArrayT<T>& b) ,
> since A is not const. b1*A= b1.add(A).
The message is send to the object at the left
b*A is equivalent to b.operator*(A)
Given that `b' is a const object, it would call a const method (those that have const at the end)
By instance const ArrayT<T>& ArrayT<T>::operator*(const ArrayT<T>& b) const
> By that way what is Alice and Bob here.
1 2 3
int answer = 42;
constint *alice = &answer;
int *bob = alice;
I just wanted to show a const method (those that have const at the end)
Again, const ArrayT<T>& ArrayT<T>::operator*(ArrayT<T>& b) const is useless.
¿will its code be any different than the const ArrayT<T>& ArrayT<T>::operator*(const ArrayT<T>& b) const version?
well if I don't include all of the operator * overloading for the const and non const version, it wont be able to compile. For example in A*B , if A is const , B is const and I don't have the
const ArrayT<T>& ArrayT<T>::operator*(const ArrayT<T>& b) const
and only have const ArrayT<T>& ArrayT<T>::operator*(ArrayT<T>& b) const
it won't run, because B is a const, it can't not be called by the above, which b is non const.
const ArrayT<T>& ArrayT<T>::operator*(const ArrayT<T>& b) const
You return a reference to.. what? The invoking object can't change - it's const. The ArrayT referred to by the parameter can't change - it's const. So what are you returning a reference to?
@cire: i think it should return a reference to an object of type const ArrayT<T>. But again, I am not really sure, that's how my instructor did in his operator+ overloading for a const A and const B when A+B. I only imitate the operator*. However, in my code, I know that if I have a const A and a const B, then A*B always calls
const ArrayT<T>& ArrayT<T>::operator*(const ArrayT<T>& b) const
@ne555: Today I used Array<T> Array<T>::operator*(const Array<T>&) const and it works like a charm now. Thanks alot. I get rid off those irrelevant prototype already.