简体   繁体   English

我的代码覆盖自身时遇到问题,我不知道为什么

[英]Having trouble with my code overwriting itself and I am not sure why

In the code below I can change the count amount newNodePtr->setCount(amount);在下面的代码中,我可以更改计数数量 newNodePtr->setCount(amount); to any value and it will stay changed.任何值,它会保持改变。 But I am trying to set it equal to one initially.但我最初试图将它设置为等于 1。 And then if it occurs again I will go to the else statement and keep my linked list value at that location but change the count by +1.然后如果它再次发生,我将转到 else 语句并将我的链表值保留在该位置,但将计数更改为 +1。

template<class ItemType>
bool LinkedBag<ItemType>::add(const ItemType& newEntry)
{

    Node<ItemType> *newNodePtr = new Node<ItemType>();
    int amount = 0;
        if (getFrequencyOf(newEntry)<1)
        {
            newNodePtr->setItem(newEntry);
            newNodePtr->setNext(headPtr);
            itemCount++;
            amount++;
            newNodePtr->setCount(amount);
            headPtr = newNodePtr;

        }
        else
        {
            const int freqAmount = getFrequencyOf(newEntry);
            newNodePtr->setItem(newEntry);
            itemCount++;
            newNodePtr->setCount(freqAmount+1);
        }

    return true;
}  // end add

In your else block, you are setting the count on the newly created item.在您的 else 块中,您正在为新创建的项目设置计数。 Not on the existing item in the list.不在列表中的现有项目上。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM