Page 1 of 1

"Can't convert arguments from initilizer to Loc" Snek Game

Posted: August 19th, 2017, 11:33 am
by sheriff40
I made the snake constructor like:
Snake::Snake(Loc& loc)
{
segments[0].Inithead(loc);
}
Then i made the object:
Snake Snek;
Now, when i want to initilize the loc i get error like:
Snek({2,2});
Cannot convert arguments from initilizer to Loc

But when I remove the reference from the defination like:
Snake::Snake(Loc loc) //No reference to loc//
{
segments[0].Inithead(loc);
}
No error is shown. What is happening here. Please help!!

Re: "Can't convert arguments from initilizer to Loc" Snek Ga

Posted: August 19th, 2017, 2:52 pm
by albinopapa
You should share source code not just snippets when asking for help.

Re: "Can't convert arguments from initilizer to Loc" Snek Ga

Posted: August 19th, 2017, 3:12 pm
by Zedtho
Especially since the errors are not often in the spot one would suspect it to be.

Re: "Can't convert arguments from initilizer to Loc" Snek Ga

Posted: August 20th, 2017, 3:19 pm
by chili
try const Loc& loc

http://ideone.com/u8LwMY

Re: "Can't convert arguments from initilizer to Loc" Snek Ga

Posted: August 21st, 2017, 11:26 pm
by reductor
The reason for this is that it is unable to determine a type, your attempting to construct a temporary, which can not be treated as a non-const reference, unfortunately MSVC is terrible for giving good error messages. Likely if you added the type before {} it would better hint at the issue.

Re: "Can't convert arguments from initilizer to Loc" Snek Ga

Posted: August 22nd, 2017, 1:12 am
by chili
Yeah, definitely much better diagnostics from gcc here. It could be worse, it could just allow you to pass a temporary by reference (which is someting MSVC permitted in previous versions I believe :lol:).

This just demonstrates another reason to const your reference parameters whenever possible: it gives you more flexibility regarding what you can pass in vs. non-const reference semantics.

Re: "Can't convert arguments from initilizer to Loc" Snek Ga

Posted: August 27th, 2017, 3:09 pm
by sheriff40
Thank you so much guys. Thank you Chilli. I have been making some progress lately.