2016-11-04 4 views
-3

このプロジェクトには、姓、名、整数、および二重で構成される個人情報のリンクリストがあります。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つのうち1つのパラメータだけを照合する方法です。私は、次のことを試してみた:

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

しかし、私は(intです)memberNumberへの電流> getDonor()(タイプDonorTypeのノード)を比較することはできませんので、もちろん、それは動作しません。それは任意のヘルプだ場合、これはヘッダファイルに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 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; 
}; 
+0

を必要としています。 DonorTypeにそのドナー名、IDなどが含まれているのは奇妙に思われますが – pm100

+0

あなたが主張するように、示されたコードには「DonorTypeクラスから継承します」というものは何もありません。 C++の基本的な概念をよく理解していないように見えます。これは、stackoverflow.comの簡単な答えのスペースで適切に処理できるものではありません。答えることができる明確な質問を伝えることができるようにするために、適切な用語と一般的な用語を使ってコミュニケーションをとることができます。 –

+2

、なぜstd :: listを使用していないのですか – pm100

答えて

3

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

ちなみに、私はchan代わりにDonerType&参照を返すようにNode::getDonor()を送信してください。そうすれば、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; 
} 
+0

興味深い!私はそれを "current-> getDonor()に変更しました。getMembershipNo()== memberNumber"あなたが提案したように、今ではエラーが発生します。C3867( 'MemberType :: getMembershipNo':非標準構文です。メンバーに)。明らかに何らかの継承問題ですか? 'int'から 'int(_thiscall MemberType:*)(void)'と '==': 'int(_thiscall MemberTyupe:*)(void);'への変換はありません。インダイレクションのレベルは「int」と異なります。ああ、これは実行時エラーだけです、デバッグしようとしない限り、コンパイラはもう文句を言っていません。 – BaloneyOs

+0

'if(current-> getDonor()。getMembershipNo()== memberNumber)'というステートメントは、それらのエラーを生成できません。おそらく、あなたは 'if(current-> getDonor()。getMembershipNo == memberNumber)'と書きました。 'getMembershipNo'に括弧がないことに注目してください。 –

+0

おっと、そのタイプミスを指摘してくれてありがとう。それは少なくとも今実行中です。 – BaloneyOs

2

あなたは私たちがDonorTypeクラスを参照してくださいする必要が

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

あなたは 'current'をインクリメントしていないので、最初のノードがマッチしない場合は、無限ループに陥ります。 –

+0

:-)私は、すべてのコードがメンバー番号をテストするビットとは別に正しいと仮定していました。 – pm100

+0

'Node :: link'は' private'で、 'DonorList'は' Node'の 'friend'ではないので、' current = current.link; 'は動作しません(とにかく' > 'の代わりに'> ')。 –

関連する問題