返回迭代器的函数缺少分号错误
Missing semicolon error for functions returning iterator
我正在尝试对间隔树进行粗略的抽象实现,但是我遇到了一个奇怪的错误。我所有返回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
即可。
相关文章:
- 错误 C2760:语法错误:映射迭代器上意外的标记"标识符",预期的";"
- 初始化迭代器错误 C++ 在 GCC 编译器中
- 迭代器类的重载前缀增量运算符会引发分段错误
- 为什么我在使用 istream 迭代器时会出现 seg 错误?
- 在静态库中使用输出迭代器时出现链接器错误
- 在分配和发布递增循环迭代器时C++无限循环(gcc 错误?
- 比较迭代器会使程序崩溃,而不会在自定义气泡排序实现中出现错误
- C++结构编译器错误询问向量的迭代器
- 编译时二叉搜索树错误的反向迭代器表示"no matching function call for operator=()"
- 编译错误 std::vector<std::shared_ptr<T>>迭代器和擦除方法
- 赛松迭代器错误
- 向量迭代器不兼容的错误,用于保存另一个向量的迭代器的向量
- C++,弹出调试断言失败窗口,我得到矢量迭代器不兼容的错误运行时
- 在 leveldb 的 c++ 示例中声明迭代器时出现分段错误
- std::map 擦除 - 将迭代器传递给错误的映射
- C++向量迭代器nth_element编译错误
- 如何修复错误,迭代器未在此范围内声明,并且迭代器未命名类型'
- 如何修复使用矢量和迭代器打印值的错误
- C++ 迭代器错误"does not refer to a value"
- 使用 -std=c++0x 会导致错误:“迭代器”未命名类型错误