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

前端 未结 3 694
花落未央
花落未央 2020-12-21 06:43

I have a function prototype like

test(something &)

and i am doing

something *ss = new something();

3条回答
  •  旧时难觅i
    2020-12-21 07:23

    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.
    

提交回复
热议问题