gpt4 book ai didi

c++ - 错误 C2872 : 'range_error' : ambiguous symbol

转载 作者:塔克拉玛干 更新时间:2023-11-03 08:14:19 24 4
gpt4 key购买 nike

我已经搜索过 SO 和谷歌,我没有在两个地方声明相同的变量,也没有以我知道的奇怪方式包含一些东西。插入方法应该可以正常工作,它是一个预先编写的方法(我想这也可能是错误的..大声笑)。这是我得到的错误。

错误:

error C2872: 'range_error' : ambiguous symbol
........ while compiling class template member function 'Error_code List<List_entry>::insert(int,const List_entry &)'

对我来说,insert 方法看起来没问题,我没有发现与 0 进行比较的 position 变量或在构造函数中声明为 0 以返回 range_error 的 count 有任何问题。

插入方法:

template <class List_entry>
Error_code List<List_entry>::insert(int position, const List_entry &x){
Node<List_entry> *new_node, *following, *preceding;
if(position < 0 || position > count){
return range_error;
}
if(position == 0){
if(count == 0) following = nullptr;
else {
set_position(0);
following = current;
}
preceding = nullptr;
}
else {
set_position(position - 1);
preceding = current;
following = preceding->next;
}

new_node = new Node<List_entry>(x, preceding, following);

if(new_node == nullptr) return overflow;
if(preceding != nullptr) preceding->next = new_node;
if(following != nullptr) following->back = new_node;

current = new_node;
current_position = position;
count++;

return success;
}

问题可能在于我没有重载 = 运算符的实现吗?

此处所有代码:pastie.org/1258159

最佳答案

range_error 在您的代码(在全局命名空间中)和标准库(在 std 命名空间中)中定义。您使用 using namespace std; 将整个标准命名空间拖入全局命名空间会产生歧义。您应该至少执行以下操作之一:

  • 从全局命名空间中删除using namespace std;在你的函数中使用命名空间,或者只使用你需要的名称,或者在你使用它们时限定所有标准名称
  • 仔细选择自己的名字,避免与标准名字冲突
  • 将您自己的名称放在命名空间中(不要将其放入全局命名空间)。

关于c++ - 错误 C2872 : 'range_error' : ambiguous symbol,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4053432/

24 4 0
文章推荐: c# - 一种语言将 undefined 评估为等于 false 是否很常见?如果是这样,为什么这样做?
文章推荐: c++ - C/C++ 中的表达式求值不遵循 BODMAS 规则?
文章推荐: c++ - 在 C++ 中对 vector 进行类型转换?