簡體   English   中英

例外'out_of_range'不是std的成員?

[英]Exception 'out_of_range' not member of std?

我正在嘗試編寫一個簡單的鏈表,並且當用戶想要一個超出范圍的節點索引時,我試圖拋出一個out_of_range異常。 但是,當我編譯源文件時,我收到錯誤“'out_of_range'不是'std'的成員”。

我的理解是'out_of_range'是std ::的成員,所以我假設我做錯了,我不知道。

這是發生錯誤的地方:

T getValueAtIndex (int index)
        {
            // If the index is less than 0 or greater than/equal to
            // the length, throw index out-of-bounds exception.
            if (index < 0 || index >= length)
            {
                throw std::out_of_range ("Index out of bounds.");
            }
            else 
            {
                // Start at index 0.
                int i = 0;
                // Start with the first node in the list (headNode).
                Node * currentNode = headNode;
                // While we have not yet reached the index we want...
                while (i < index)
                {
                    currentNode = currentNode->getNextNode();
                    i++;
                }
                return currentNode->getValue();
            }
        }

到目前為止,這是整個文件(尚未完成):

#include <iostream>

template <typename T>
class LinkedList
{
    // Private variables/information.
    private:
        // Struct 'Node' which holds the data and a pointer to the next
        // node in the linked-list.
        struct Node {
            T value;
            Node * nextNode;
            // Node constructor, sets nextNode to NULL.
            Node()
            {
                nextNode = NULL;
            }
            // Sets the value of this node to the value passed to it.
            void setValue (T Value)
            {
                value = Value;
            }
            void setNextNode (Node * newNode)
            {
                nextNode = newNode;
            }
            // Returns the value of this node.
            T getValue()
            {
                return value;
            }
            // Returns a pointer to the next node in the list.
            Node * getNextNode()
            {
                return nextNode;
            }
        };
        // The head or 'first' node in the list.
        Node * headNode;
        // The tail or 'last' node in the list.
        Node * tailNode;
        // Length of the list. Useful for making sure we 
        // do not search for a node index that is out of bounds.
        int length;
    // Public methods.
    public:
        // Default constructor for the linked list.
        // Initializes the two nodes to NULL.
        LinkedList()
        {
            headNode = NULL;
            tailNode = NULL;
            length = 0;
        }
        // Adds a value to the linked-list as a node.
        void add (T Value)
        {
            // Create a new node on the heap.
            Node newNode = new Node();
            // Set the value of this new node to the value specified.
            newNode.setValue(Value);

            // If there is no node in the list yet...
            if (headNode == NULL)
            {
                // Point headNode and tailNode to the address of newNode.
                headNode = &newNode;
                tailNode = &newNode;
            }
            // Else if there is already a node in the list.
            else
            {
                // Link the new node to the last current node.
                tailNode->setNextNode(&newNode);
                // Set the last node to the new node.
                tailNode = &newNode;
            }

            // Increase the length of the list by one.
            length++;
        }
        // Returns the value of the node at the given index.
        // Starts at index 0 like a normal array.
        T getValueAtIndex (int index)
        {
            // If the index is less than 0 or greater than/equal to
            // the length, throw index out-of-bounds exception.
            if (index < 0 || index >= length)
            {
                throw std::out_of_range ("Index out of bounds.");
            }
            else 
            {
                // Start at index 0.
                int i = 0;
                // Start with the first node in the list (headNode).
                Node * currentNode = headNode;
                // While we have not yet reached the index we want...
                while (i < index)
                {
                    currentNode = currentNode->getNextNode();
                    i++;
                }
                return currentNode->getValue();
            }
        }
};

int main()
{


    return 0;
}

拋出異常時我做錯了什么? 我試過沒有'std ::',包括和前面的'new'。

看起來你只是忘了#include <stdexcept>

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM