返回迭代器的函数缺少分号错误

Missing semicolon error for functions returning iterator

本文关键字:错误 迭代器 函数 返回      更新时间:2023-10-16

我正在尝试对间隔树进行粗略的抽象实现,但是我遇到了一个奇怪的错误。我所有返回std::map<ui16, Node>::iterator的函数都给了我错误:

错误

C2146:语法错误:标识符"FOO"之前缺少";">

其中FOO是函数的名称。

ui16只是一个typedef unsigned short.

有什么想法吗?

#include "stdafx.h"
#include <map>
//Very rough implementation of a specialized interval tree
//TODO: Replace std::map with a custom red-black tree implementation or something similar
template <typename T>
class IntervalTree {
public:
    struct Node {
        Node(T Data, ui16 Length) : data(Data), length(Length) {}
        T data;
        ui16 length;
    };
    inline void clear() {
        std::map<ui16, Node>().swap(_tree);
    }
    inline std::map<ui16, Node>::iterator getInterval(ui16 index) const {
        auto it = _tree.lower_bound(index);
        if (it->first != index) {
            return it--;
        }
        return it;
    }
    inline std::map<ui16, Node>::iterator insert(std::pair<ui16, Node> pair) {
        return _tree.insert(pair);
    }
    std::map<ui16, Node>::iterator insert(ui16 index, T data) {
        //Find containing interval
        auto it = _tree.lower_bound(index);
        if (it->first != index) {
            it--;
        }
        //Its already in the tree
        if (it->second.data == data) {
            return it;
        }
        //If the interval is only 1, just change its value
        if (it->second.length == 1) {
            it->second.data = data;
            return it;
        }
        //Check if its at the right edge of the interval
        if (index == it->first + it->second.length - 1) {
            auto next = it + 1;
            //if it should add to the next interval
            if (data == next->second.data) {
                it->second.length--;
                it = _tree.insert(make_pair(index, Node(data, next->second.length + 1)));
                _tree.erase(next);
                return it;
            } else { //else we should add an interval and modify left interval
                //Modify left interval
                it->second.length--;
                //Insert new interval
                return _tree.insert(make_pair(index, Node(data, 1)));
            }
        } else if (index == it->first) { //if its at the beginning of the interval
            //Insert right interval
            _tree.insert(make_pair(index + 1, Node(it->second.data, it->second.length - 1)));
            _tree.erase(it);
            _tree.insert(make_pair(index, Node(data, 1)));
        } else { //else its in the middle of the interval
            //Insert right interval
            _tree.insert(make_pair(index + 1, Node(it->second.data, it->second.length - (index - it->first) - 1)));
            //Modify left interval
            it->second.length = index - it->first;
            //Insert new middle interval
            return _tree.insert(make_pair(index, Node(data, 1)));
        }
        return _tree.end();
    }
    inline std::map<ui16, Node>::iterator begin() const { return _tree.begin(); }
    inline std::map<ui16, Node>::iterator end() const { return _tree.end(); }
private:
    std::map <ui16, Node> _tree;
};

C2146 给出了一个相当无用的错误消息,但最终问题是你错过了存在依赖作用域的 typename 关键字。 iterator取决于std::map<ui16, Node>的实例化,并且需要 typename 关键字来消除您的用法歧义。有关更多信息,请参阅我必须在哪里以及为什么必须放置"模板"和"类型名称"关键字?。有关更有用的错误消息,请参阅 gcc 中的以下示例:

main.cpp:83:12: error: need ‘typename’ before 
‘std::map<short unsigned int, IntervalTree<T>::Node>::iterator’ 
because ‘std::map<short unsigned int, IntervalTree<T>::Node>’ is a dependent scope
     inline std::map<ui16, Node>::iterator begin() const { return _tree.begin(); }

只需将std::map<ui16, Node>::iterator替换为typename std::map<ui16, Node>::iterator即可。