c++如何从具有多个参数的节点中检索信息



对于这个项目,有一个由名、姓、整数和双精度组成的个人信息链表。它看起来像这样:

void DonorList::addDonor(string firstName, string lastName, int memberID, double donation)
{
    Node *pNode = new Node(DonorType(firstName, lastName, memberID, donation), nullptr);
    if (first == nullptr)
    {
        first = pNode;
        last = pNode;
    }
    else
    {
        last->setLink(pNode);
        last = pNode;
    }
}

它继承自DonorType类。我所困惑的是如何在搜索函数的4个参数中只匹配一个参数。我尝试了以下方法:

bool DonorList::searchID(int memberNumber)
{
    Node *current = first;
    bool found = false;
    while (current != nullptr || !found)
    {
        if (current->getDonor() == memberNumber)
        {
            found = true;
        }
    }
    return found;
}

但当然它不起作用,因为我不能比较current->getDonor() (DonorType类型的节点)到memberNumber(这是一个int)。下面是头文件中的Node类:

class Node
{
public:
    Node(){}
    Node(const DonorType& theDonor, Node *theLink) 
        : donor(theDonor), link(theLink){}
    Node* getLink( ) const { return link; }
    DonorType getDonor( ) const { return donor; }
    void setDonor(const DonorType& theDonor) 
        { donor = theDonor; }
    void setLink(Node *theLink) { link = theLink; }
private:
    DonorType donor;        
    Node *link;     //pointer that points to next node
};

所以现在我非常迷失在如何访问节点中的memberNumber信息以便执行搜索函数。任何帮助将非常感激,谢谢!这个项目实际上有更多的文件,但我尽了最大的努力,包括我认为是相关的问题。

编辑:这是DonorTypen类:
class DonorType : public MemberType
{
public:
    DonorType();
    DonorType(const string& firstName, const string& lastName, const int& memberNumber, const double& donationAmount);
    void setDonorInfo(string &firstName, string &lastName, int &memberNumber, double &donationAmount);
    void setAmountDonated(double &donationAmount);
    double getAmountDonated() const;
    void printDonor() const;
    void printDonation() const;
    ~DonorType();
private:
    double donation;
};

Edit2: MemberType class

class MemberType
{
public:
    MemberType();
    MemberType(const string& firstName, const string& lastName, const int& memberNumber);
    void setMemberInfo(const string& firstName, const string& lastName, const int& memberNumber);
    string getFirstName() const;
    string getLastName() const;
    int getMembershipNo();
    void printName() const;
    void printMemberInfo() const;
    ~MemberType();
private:
    string fname;
    string lname;
    int idnum;
};

Node::getDonor()返回DonorType对象,该对象具有访问单个值的方法,例如:

bool DonorList::searchID(int memberNumber)
{
    Node *current = first;
    while (current)
    {
        if (current->getDonor().getMembershipNo() == memberNumber)
        {
            return true;
        }
        current = current->getLink(); // <-- you also need to add this!
    }
    return false;
}

在旁注上,我建议将Node::getDonor()更改为返回DonerType&引用。这样,你就不会在每次调用getDonor()时都创建数据副本,而且node->getDonor().set...()之类的东西也会按预期工作。

同样,您的addDonor()实现可以简化为:

void DonorList::addDonor(string firstName, string lastName, int memberID, double donation)
{
    Node *pNode = new Node(DonorType(firstName, lastName, memberID, donation), nullptr);
    if (!first)
        first = pNode;
    if (last)
        last->setLink(pNode);
    last = pNode;
}

你需要

bool DonorList::searchID(int memberNumber)
{
    Node *current = first;
    bool found = false;
    while (current != nullptr || !found)
    {
        if (current->getDonor().getMembershipNo() == memberNumber)
        {
            found = true;
        }
        else current = current->getLink();
    }
    return found;
}

相关内容

  • 没有找到相关文章

最新更新