Lasagna-master Task 3 not passing

Hello,

I am trying to solve lasagna-master exercise, but I am not able to mark Task 3 as solved. It seems that the function is applied but do nothing.

This is its definition:

void addSecretIngredient(std::vector<std::string> myList, const std::vector<std::string> friendsList){
    myList.pop_back();
    myList.emplace_back(friendsList.back());
}

I used the same code for Task 5 and in that case it works.

What am I missing?

Thanks in advance,
Fabio

1 Like

The function in your snippet takes its first argument “by value”.
That means the parameter myList gets constructed from the first argument that the caller passes to the function and gets destructed at the end of the function. No modification of myList is visible from the outside or does affect the argument of the caller.

Instead, the function should take its first argument “by reference”. Then the parameter myList is a reference to the argument of the caller, modifying the parameter inside the function will modify the argument of the caller.

You might want to read Chapter 12.5 — Pass by lvalue reference at LearnCpp.com.

2 Likes

Thanks @siebenschlaefer, your suggestion solved the problem.
Actually, I was biased by the fact that in the Instruction it seems that two strings are accepted as arguments, not a reference to a string and a string, as specified in Task 5.

1 Like

If a function return type is void, typically its calculation can only affect your program as a side effect. It usually means that the function modifies a variable passed as a reference, as a pointer, or otherwise visible to the function and modifiable by it, although not local to it.

1 Like

The exercise is very new, so it might be a bit rough.
If you have any suggestions on better wording that would help students (without outright telling them the solution), we are all ears.

Thanks @vaeng for the suggestion.
Eventually, where could I propose the change of the instrunctions text?

You can do it right here or if you prefer a PR, github is fine as well.

1 Like