Hello,
I am getting a segmentation fault when I click the “Run Test” button on the website for the Lasagna Master exercise. I can test the code locally on my computer and all the tests pass.
### We received the following error when we ran your code:
make[2]: *** [CMakeFiles/test_lasagna-master.dir/build.make:70: CMakeFiles/test_lasagna-master] Segmentation fault (core dumped)
make[1]: *** [CMakeFiles/Makefile2:111: CMakeFiles/test_lasagna-master.dir/all] Error 2
make: *** [Makefile:91: all] Error 2
Testing the code locally.
$ make
[ 25%] Building CXX object CMakeFiles/lasagna-master.dir/lasagna_master_test.cpp.o
[ 50%] Building CXX object CMakeFiles/lasagna-master.dir/lasagna_master.cpp.o
[ 75%] Building CXX object CMakeFiles/lasagna-master.dir/test/tests-main.cpp.o
[100%] Linking CXX executable lasagna-master
[100%] Built target lasagna-master
===============================================================================
All tests passed (1 assertion in 1 test case)
[100%] Built target test_lasagna-master
Here is the code!
#include "lasagna_master.h"
#include <vector>
#include <algorithm>
namespace lasagna_master {
int preparationTime(std::vector<std::string> layers, int minutes) {
return layers.size() * minutes;
}
amount quantities(const std::vector<std::string> items) {
amount a = {0, 0.0};
for (auto& i : items) {
if (i == "noodles") {
a.noodles += 50;
}
if (i == "sauce") {
a.sauce += 0.2;
}
}
return amount{a.noodles, a.sauce};
}
void addSecretIngredient(std::vector<std::string>& myList, const std::vector<std::string> friendsList) {
myList[myList.size() - 1] = friendsList.back();
}
std::vector<double> scaleRecipe(std::vector<double> portions, int scaleTo) {
std::vector<double> newPortions = portions;
for (const auto& i : portions) {
newPortions[i] = i * scaleTo;
}
return newPortions;
}
void addSecretIngredient(std::vector<std::string>& myList, std::string secret) {
myList[myList.size() - 1] = secret;
}
} // namespace lasagna_master
Header file.
#pragma once
#include <string>
#include <vector>
namespace lasagna_master {
struct amount {
int noodles;
double sauce;
};
int preparationTime(std::vector<std::string> layers, int time=2);
amount quantities(std::vector<std::string>);
void addSecretIngredient(std::vector<std::string>& myList, const std::vector<std::string> friendsList);
std::vector<double> scaleRecipe(std::vector<double>, int scaleTo);
void addSecretIngredient(std::vector<std::string>& myList, std::string secret="?");
} // namespace lasagna_master