【问题标题】:C++ List within Class to store user input类中的 C++ 列表以存储用户输入
【发布时间】:2016-08-09 08:42:48
【问题描述】:

自从我上次编写代码以来已经有一段时间了,但我正在努力整理一下我在学习时获得的一些技能。现在,我只是想为我在网上看到的陈述/问题实施解决方案。

为此,我正在尝试构建一个过敏类来存储用户输入提供的信息(类别、名称、症状)。我开始只是为每个参数输入字符串,但在现实世界中,人们可能有多种症状。为此,我想为症状创建一个列表参数而不是单个字符串。这是我的文件:

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 中的列表创建一个吸气剂。非常感谢任何指导!!!

【问题讨论】:

    标签: c++ list class parameters getter


    【解决方案1】:

    getter 实现的签名与类定义中的签名不匹配:

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

    纠正一下:

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

    编辑:

    即使现在可以编译 getter,你也不能像这样只显示症状列表:

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

    要打印症状,可以使用 range-for,这是一种遍历列表的简单方法:

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

    或者使用带有 ostrea_iterator 的副本:

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

    【讨论】:

    • 谢谢!!我在发布后不久就发现了 getter 错误-doh。感谢您提供有关打印列表的专业提示。你能再解释一下for循环吗? - 它确实有效,但我不想使用我不理解的东西。
    • for-range 使用内容变量(这里是 s,我不想打扰类型,所以我把 auto )。更多信息:en.cppreference.com/w/cpp/language/range-for
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多