繁体   English   中英

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

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

在下面的代码中,我可以更改计数数量 newNodePtr->setCount(amount); 任何值,它会保持改变。 但我最初试图将它设置为等于 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

在您的 else 块中,您正在为新创建的项目设置计数。 不在列表中的现有项目上。

暂无
暂无

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

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