【问题标题】:Exception 'out_of_range' not member of std?异常'out_of_range'不是std的成员?
【发布时间】:2015-04-13 14:14:34
【问题描述】:

我正在尝试编写一个简单的链表,并在用户想要一个超出范围的节点索引时尝试抛出一个 out_of_range 异常。但是,当我编译源文件时,我得到一个错误“'out_of_range' is not a member of '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'。

【问题讨论】:

  • 如此远非minimal测试用例,伙计... :(
  • 我认为它可能比它需要的更复杂......我的代码往往是当我尝试新事物时。 :(
  • 好的,但这正是编写最小测试用例的原因!!

标签: c++ compiler-errors outofrangeexception


【解决方案1】:

你好像忘了#include &lt;stdexcept&gt;

【讨论】:

  • 谢谢你,没有意识到我需要那个(愚蠢的我)。当 StackOverflow 允许我时,我会在 9 分钟内接受你的回答。 <.>
猜你喜欢
  • 1970-01-01
  • 2017-11-26
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-09-16
相关资源
最近更新 更多