std::string allergy_labels[8] = {"eggs","peanuts","shellfish","strawberries","tomatoes","chocolate","pollen","cats"};
bool allergy_flags[8] = {false,false,false,false,false,false,false,false};
bool allergy_test::is_allergic_to(std::string allergen)
{
for (int i = 0 ; i < 8 ; i++)
{
if (allergy_labels[i].compare(allergen) == 0)
{
return allergy_flags[i];
}
}
return false;
}
So I define the two arrays as you see above, then when I try to return an entry from allergy_flags (which is a boolean array) within is_allergic_to I get an error that strings cannot be converted to booleans, which is fair but I can’t see anywhere I’m doing it.
If anyone can see where I’m being dense about this I’d very much appreciate it being pointed out, but I’ve asked people with more experience and even Phind, none of whom can see the error if it exists.
Could you share your entire code file and the complete error message?
Certainly.
#if !defined(ALLERGIES_H)
#define ALLERGIES_H
#include <string>
#include <unordered_set>
namespace allergies {
class allergy_test{
private:
std::string allergy_labels[8];
std::string allergy_flags[8];
public:
allergy_test(int);
bool is_allergic_to(std::string);
std::unordered_set<std::string> get_allergies();
};
} // namespace allergies
#endif // ALLERGIES_H
#include "allergies.h"
namespace allergies {
std::string allergy_labels[8] = {"eggs","peanuts","shellfish","strawberries","tomatoes","chocolate","pollen","cats"};
bool allergy_flags[8] = {false,false,false,false,false,false,false,false};
allergy_test::allergy_test(int score)
{
int n = 0;
int k = 1;
while (k <= 128 && score > 0)
{
if (score%(2*k) == k)
{
allergy_flags[n] = true;
score -= k;
}
n++;
k *= 2;
}
}
bool allergy_test::is_allergic_to(std::string allergen)
{
for (int i = 0 ; i < 8 ; i++)
{
if (allergy_labels[i].compare(allergen) == 0)
{
return allergy_flags[i];
}
}
return false;
}
std::unordered_set<std::string> allergy_test::get_allergies()
{
std::unordered_set<std::string> list = {};
for (int i = 0 ; i < 8 ; i++)
{
if (allergy_flags[i])
{
list.insert(allergy_labels[i]);
}
}
return list;
}
} // namespace allergies
And the error is:
We received the following error when we ran your code:
/tmp/allergies/allergies.cpp: In member function 'bool allergies::allergy_test::is_allergic_to(std::string)':
/tmp/allergies/allergies.cpp:30:43: error: cannot convert 'std::string' {aka 'std::__cxx11::basic_string<char>'} to 'bool' in return
30 | return allergy_flags[i];
| ~~~~~~~~~~~~~~~^
| |
| std::string {aka std::__cxx11::basic_string<char>}
/tmp/allergies/allergies.cpp: In member function 'std::unordered_set<std::__cxx11::basic_string<char> > allergies::allergy_test::get_allergies()':
/tmp/allergies/allergies.cpp:42:36: error: could not convert '((allergies::allergy_test*)this)->allergies::allergy_test::allergy_flags[i]' from 'std::string' {aka 'std::__cxx11::basic_string<char>'} to 'bool'
42 | if (allergy_flags[i])
| ~~~~~~~~~~~~~~~^
| |
| std::string {aka std::__cxx11::basic_string<char>}
make[2]: *** [CMakeFiles/allergies.dir/build.make:90: CMakeFiles/allergies.dir/allergies.cpp.o] Error 1
make[1]: *** [CMakeFiles/Makefile2:85: CMakeFiles/allergies.dir/all] Error 2
make: *** [Makefile:91: all] Error 2
And as I post that I realise I defined that array as a string array in the header, nevermind! Another lesson learned
3 Likes