Please Help with "invalid initialization of reference of type"

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

template <class T>
const ArrayT<T>& ArrayT<T>::operator*(const ArrayT<T>& b) const

template <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.
Last edited on
The reference always refers to an existing object. Return by value.
But that's my requirement, I was success to return by value for that c2. is there anyway to go around it?


Basically here is what I need to do, I have success for most of them except the above.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
const ArrayT<float>& b1 = A*A;
const ArrayT<float>& b2 = b1*A;
const ArrayT<float>& b3 = A*b1;
ArrayT<float>& c1 = A*A

;
ArrayT<float>& c2 = b1*A;// <-------- Trouble here


ArrayT<float>& c3 = A*b1;
const ArrayT<float> d1 = 2.*A*A;
const ArrayT<float> d2 = 3.*b1*A;
const ArrayT<float> d3 = A*b1;
const ArrayT<float>& e1 = 3.f*d1*A*b1;
const ArrayT<float>& e2 = 2.f*c1*A*b1;
const ArrayT<float>& e3 = A*c1*b1;
const ArrayT<float>& f1 = d1*A*b1;
const ArrayT<float>& c4 = d1*A*b1;
const ArrayT<float>& c5 = A*d1*b1;
const ArrayT<float>& f1 = d1*A + b1*b3*e2 + 3.*c4;
const ArrayT<float>& f2 = 2.*A*A;
Last edited on
> 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).
Last edited on
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;
const int *alice = &answer;
int *bob = alice;
¿better?
@cire : Nothing in the code is operator+=
@ne555: Thanks a lot, however why don't it calls the following :

const ArrayT<T>& ArrayT<T>::operator*(ArrayT<T>& b) const

the inside of the () ,b, is a non const , which is similar to A. in b.operator*(A).
Last edited on
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?
@ne555: Yes, for the

const ArrayT<T>& ArrayT<T>::operator*(ArrayT<T>& b) const will be called when I perform A*B,


which A is const ArrayT<float>& , B is ArrayT<float>&. In this case T is the template for float .


const ArrayT<T>& ArrayT<T>::operator*(const ArrayT<T>& b) const

will be called when A is const ArrayT<float>& , B is const ArrayT<float>&
Last edited on
¿So? ¿will the result be different? ¿will the procedure to obtain the result be different? ¿will there be different side effects?
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.
ne555 meant:
Will the operations be different?
Will the non-const reference version act differently?
If it won't, just use the const version.
I'm curious what's happening in:

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
Last edited on
@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.
Topic archived. No new replies allowed.