Search code examples
c++visual-studio-2010pointersambiguous

error C2872: 'range_error' : ambiguous symbol


I have already searched SO and google, I am not declaring the same variable in two places nor am I including something in a weird way..that I know of. The insert method should be working fine, it's a pre-written method(i guess that could be wrong too.. lol). This is the error I get.

Error:

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

To me the insert method looks alright, I don't see any problems with the position variable which is being compared to 0 or count which is declared as 0 in the constructor to return the range_error.

Insert Method:

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;
}

Could the problem be in that I don't have an implementation of the overloaded = operator?

All code here: pastie.org/1258159


Solution

  • range_error is defined both in your code (in the global namespace) and by the Standard Library (in the std namespace). Your use of using namespace std; to drag the entire Standard namespace into the global namespace creates an ambiguity. You should do at least one of the following:

    • remove using namespace std from the global namespace; either use the namespace within your functions, or use just the names you need, or qualify all the standard names when you use them
    • carefully choose your own names to avoid conflicts with standard names
    • place your own names inside a namespace (and don't pull that into the global namespace).