views:

45

answers:

4

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

+1  A: 

range_error is a class defined in the stdexcept header. In your code you probably use a constant with the same name and make std::range_error visible with an using namespace std; directive which results in the ambiguity. Either rename your constant or revise your using directives.

For example:

#include <stdexcept>

using namespace std;

int range_error = 42;

int main()
{
    return range_error;
}

The code above causes the same error.

vitaut
there is only one range_error declaration and implementation. See here http://pastie.org/1258159
Knownasilya
Right, but there is another in the standard header, so I would recommend you replacing `using namespace std;` with only using declaration that you need, e.g. `using std::cout;`, etc. Alternatively rename your `range_error` to something else, e.g. `out_of_range`.
vitaut
That's exactly what I did. :)
Knownasilya
@Knownasilya: Good!
vitaut
+2  A: 

Your using namespace std; statement at the top brings in declaration of std::range_error exception from stdexcept header. This conflicts with your enum value. Enclose the enum in a struct and fully qualify:

struct my_errors {
    enum {
        // ...
        range_error
    };
};
// ...
return my_errors::range_error;

Or just avoid using names from the standard library.

Nikolai N Fetissov
If I could vote your answer, I would. Thanks for the suggestion. In this situation I can't change the name of Error_code. But when I can i'll be sure to keep this in mind.
Knownasilya
+3  A: 

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).
Mike Seymour
Thank you for the amazing answer! I actually learned something!
Knownasilya
+1  A: 

An alternative solution (in addition to those already given) is to qualify the name, ::range_error. That's what namespaces are for. It's why the standard lib stuff is down in namespace std, so that such conflicts can be very easily resolved.

In the code presented above,

if(new_node == nullptr) return overflow;

will never execute the return.

Can you see why?

Cheers & hth.,

PS: Re the pastebook code, note that main must have result type int, not void. Also, many functions fail to return a value.

Alf P. Steinbach