compiler error saying invalid initialization of reference of type something& from expression of type something*

compiler complains saying initialization of reference of type something& from expression something * . but isn't that new returns the address and ss must point to that address ! so if test is expecting a reference is not it ss represents a reference ?

asked Feb 12, 2013 at 0:29 397 1 1 gold badge 5 5 silver badges 16 16 bronze badges

3 Answers 3

Your function expects a normal something object. You don't need to use a pointer here:

something ss; test(ss); 

When your function signature looks like f(T&) , it means that it accepts a reference to a T object. When the signature is f(T*) , it means that it accepts a pointer to a T object.

answered Feb 12, 2013 at 0:31 96.4k 41 41 gold badges 171 171 silver badges 253 253 bronze badges

Your function expects a reference to something , and you are passing it a pointer to something . You need to de-reference the pointer:

test(*ss); 

That way the function takes a reference to the object pointed at by ss . If you had a something object, you could pass that directly to:

something sss; test(sss); // test takes a reference to the sss object.