#C++ Lasagna Master Type inconsistencies
12 messages · Page 1 of 1 (latest)
lasagna-master.cpp: ```#include "lasagna_master.h"
namespace lasagna_master {
int preparationTime(std::vectorstd::string layers, int prep_time_per_layer) {
return layers.size() * prep_time_per_layer;
}
amount quantities(std::vector<std::string> layers) {
amount toReturn; toReturn.noodles = 0; toReturn.sauce = 0;
for (int i{0}; i < layers.size(); i++) {
if (layers.at(i) == "noodles") {
toReturn.noodles += 50;
}
else if (layers.at(i) == "sauce") {
toReturn.sauce += 0.2;
}
}
return toReturn;
}
void addSecretIngredient(std::vector<std::string>& curr_ingredients, const std::vector<std::string> needed_ingredients) {
curr_ingredients.back() = needed_ingredients.back();
}
std::vector<double> scaleRecipe(std::vector<double> base, int portions) {
std::vector<double> scaled;
for (int i{0}; i < base.size(); i++) {
scaled.emplace_back(base.at(i) / 2 * portions);
}
return scaled;
}
void addSecretIngredient(std::vector<std::string>& ingredients, std::string secretIngredient) {
ingredients.emplace_back(secretIngredient);
}
} // namespace lasagna_master
lasagna-master.h: ```#pragma once
#include <vector>
#include <string>
namespace lasagna_master {
struct amount {
int noodles;
double sauce;
};
int preparationTime(std::vector<std::string> layers, int prep_time_per_layer = 2);
amount quantities(std::vector<std::string> layers);
void addSecretIngredient(std::vector<std::string>& curr_ingredients, const std::vector<std::string> needed_ingredients);
std::vector<double> scaleRecipe(std::vector<double> base, int portions);
void addSecretIngredient(std::vector<std::string>& ingredients, std::string secretIngredient);
} // namespace lasagna_master
error output: /tmp/lasagna-master/lasagna_master.cpp: In function 'lasagna_master::amount lasagna_master::quantities(std::vector<std::__cxx11::basic_string<char> >)': /tmp/lasagna-master/lasagna_master.cpp:10:26: error: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::__cxx11::basic_string<char> >::size_type' {aka 'long unsigned int'} [-Werror=sign-compare] 10 | for (int i{0}; i < layers.size(); i++) { | ~~^~~~~~~~~~~~~~~ /tmp/lasagna-master/lasagna_master.cpp: In function 'std::vector<double> lasagna_master::scaleRecipe(std::vector<double>, int)': /tmp/lasagna-master/lasagna_master.cpp:27:26: error: comparison of integer expressions of different signedness: 'int' and 'std::vector<double>::size_type' {aka 'long unsigned int'} [-Werror=sign-compare] 27 | for (int i{0}; i < base.size(); i++) { | ~~^~~~~~~~~~~~~ cc1plus: all warnings being treated as errors make[2]: *** [CMakeFiles/lasagna-master.dir/build.make:90: CMakeFiles/lasagna-master.dir/lasagna_master.cpp.o] Error 1 make[1]: *** [CMakeFiles/Makefile2:85: CMakeFiles/lasagna-master.dir/all] Error 2 make: *** [Makefile:91: all] Error 2
error: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::__cxx11::basic_string<char> >::size_type' {aka 'long unsigned int'}
Can you change one of those types to match the other?
If you can't change the type (which you should be able to), you can cast. But that shouldn't be needed.