2016-04-17 15 views
1

最後にコードを書いてからしばらく時間がありましたが、私は勉強中に得たいくつかのスキルをほこりから取り除こうとしています。現時点では、私はオンラインで見ているステートメント/質問に対するソリューションを実装しようとしています。ユーザー入力を格納するクラス内のC++リスト

私は、ユーザー入力によって提供される情報(カテゴリ、名前、症状)を格納するアレルギークラスを構築しようとしています。私は各パラメータに文字列を入力するだけで始めましたが、現実世界では複数の症状があるかもしれません。そのために、私は、単一の文字列の代わりに、症状のリストパラメータを作成したい。ここに私のファイルは、以下のとおりです。

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; 
} 

私はそれを行っていませんmain.cppの実装。今のところ私はAllergy.cpp内のリストのゲッターを作成していません。どんな指針も大変ありがとうございます!

答えて

1

ゲッターの実装の署名は、クラス定義で署名と一致しません:

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

編集:

たとえ

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

はただ、この問題を修正しますgetterがコンパイルされると、次のような症状のリストを表示することはできません:

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

のいずれか、症状を印刷範囲-ため、リストを反復処理するための簡単な方法で使用するには:

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

それともostrea_iteratorでコピーを使用します。

auto mylist=Allergy_1.getSymptom(); 
copy (mylist.begin(), mylist.end(), ostream_iterator<string>(cout," ")); 
+0

をありがとうございました!!投稿した直後にゲッターエラーが発生しました。リストを印刷する際のプロのヒントをありがとう。 forループをもう少し説明できますか? - それは挑戦的ですが、私は理解できないものを使用したくありません。 – user1748681

+1

for-rangeはコンテント変数を持つコンテナの要素( ':'の後ろ)をループします(ここでは、私はタイプを気にしたくないので、autoを入れます)。詳細はこちら:http://ja.cppreference.com/w/cpp/language/range-for – Christophe

関連する問題