1

It's been some time since I last wrote code, but I'm trying to dust off the few skills I had gained while studying. For now, I'm just trying to implement solutions to statements/questions I see online.

For this I'm trying to build an allergy class that will store information (category, name, symptoms) provided by user input. I started by just taking string input for each parameter, but in the real world, people may have multiple symptoms. For that, i want to create a list parameter for symptoms instead of a single string. Here are my files:

Allergy.hpp:

 #ifndef Allergy_hpp #define Allergy_hpp #include <iostream> #include <string> #include <list> using namespace std; class Allergy { public: Allergy(); Allergy(string, string, list <string>); ~Allergy(); //getters string getCategory() const; string getName() const; list <string> getSymptom() const; private: string newCategory; string newName; list <string> newSymptom; }; #endif /* Allergy_hpp */ 

Allergy.cpp:

#include "Allergy.hpp" Allergy::Allergy(string name, string category, list <string> symptom){ newName = name; newCategory = category; newSymptom = symptom; } Allergy::~Allergy(){ } //getters string Allergy::getName() const{ return newName; } string Allergy::getCategory() const{ return newCategory; } list Allergy::getSymptom() const{ return newSymptom; } 

main.cpp:

#include <iostream> #include <string> #include "Allergy.hpp" using namespace std; int main() { string name; string category; string symptom; cout << "Enter allergy name: "; getline(cin, name); cout << "Enter allergy category: "; getline(cin, category); cout << "Enter allergy symptom: "; getline(cin, symptom); Allergy Allergy_1(name, category, symptom); cout << endl << "Allergy Name: " << Allergy_1.getName() << endl << "Allergy Category: " << Allergy_1.getCategory() << endl << "Allergy Symptom: " << Allergy_1.getSymptom() << endl; return 0; } 

I haven't made it to the implementation in main.cpp. For now I'm stuck creating a getter for the list within Allergy.cpp. Any guidance is greatly appreciated!!!

1 Answer 1

1

The signature of the getter's implementation doesn't match the signature in the class definition:

list Allergy::getSymptom() const{ // <=== oops!! return newSymptom; } 

Just correct this:

list<string> Allergy::getSymptom() const{ // <=== yes !! return newSymptom; } 

Edit:

Even if the getter will now compile, you can't just display the list of symptoms like this:

cout << endl << "Allergy Name: " << Allergy_1.getName() << endl << "Allergy Category: " << Allergy_1.getCategory() << endl << "Allergy Symptom: " << Allergy_1.getSymptom() << endl; 

To print the symptoms, either use a range-for, which is an easy way to iterate through a list:

for (auto& s : Allergy_1.getSymptom()) { cout << s<<" "; } 

Or use a copy with an ostrea_iterator:

auto mylist=Allergy_1.getSymptom(); copy (mylist.begin(), mylist.end(), ostream_iterator<string>(cout," ")); 
Sign up to request clarification or add additional context in comments.

2 Comments

Thank you!! I caught the getter error shortly after posting - doh. Thanks for the pro tip on printing the list. Can you explain the for loop a little more? - it defiantly works, but I don't want to use something I don't understand.
The for-range loops through the elements of a container (after the :) with a content variable (here s, and I don't want to bother with the type, so I put auto). More info here: en.cppreference.com/w/cpp/language/range-for

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.