views:

107

answers:

4

I can't figure out why i'm getting an ambiguous error. This is a sample code of what I have:

public class MyString{
    //Data:
    private char[] theString;
    //constructors:
    public MyString(){ // default constructor
    }
    public MyString(String s){ // parameterized constructor
    }
    public MyString(char[] s){ // parameterized constructor
    }
    public MyString(MyString s){ // copy constructor
    }
    //A method that calls a constructor:
    public MyString foobar(){
        return new MyString(theString);
    }
}

The above generates this error when foobar() is called from somewhere else:

./MyString.java:15: reference to MyString is ambiguous, both method MyString(char[])
in MyString and method MyString(theString) in MyString match
                return new MyString(theString);
              ^

Any ideas why?

+1  A: 

I can't reproduce your error.

But this kind of error basically happens when you try to call a function and the compiler doesn't have enough type information to determine which method to call.

e.g. if you do:

MyString tricky=new MyString(null);

Then the compiler doesn't know whether null is meant to be a char[] or a String or a MyString, so you get an ambiguous error message.

The usual way to fix this is to add an explicit cast, e.g.:

MyString tricky=new MyString((String)null);

Will work....

mikera
A: 

the link is a bit dated, but I think is still applicable: http://java.sun.com/docs/books/jls/second_edition/html/expressions.doc.html#18428

chrismh
Thanks for the link. I indeed read about that earlier, but it turns out that is not the problem. The *same* code is working in windows!, it just didn't work in Linux! I'll have to check it out when i get back from school.
trusktr
A: 

I think it's a compiler bug, I cannot reproduce it. What version of JDK and OS are you using?

Also, can you share the code from where you are calling foobar()

zengr
Zengr, see the links above. I've posted the code on snipplr. The code here was *similar*.
trusktr
A: 

Aha!! I found the problem. Lines 260 and 261 of my code were accidentally switched so the compiler was looking at my return function as a method.

It would be the equivalent of changing the above sample code to this erroneous version:

public class MyString{
    //Data:
    private char[] theString;
    //constructors:
    public MyString(){ // default constructor
    }
    public MyString(String s){ // parameterized constructor
    }
    public MyString(char[] s){ // parameterized constructor
    }
    public MyString(MyString s){ // copy constructor
    }
    //A method that calls a constructor:
    public MyString foobar(){
    }
        return new MyString(theString); //THIS LINE WAS OUT OF PLACE! SHOULD BE IN FOOBAR!
}
trusktr
But that should have given you a different compiler error. An illegal start of type.
zengr
Actaully, when compiling my main() program, it didn't give that error, only the ambiguous error. Then, when I tried compiling just my MyString class by its self, it indeed gave me that error. Is it supposed to give the same error no matter what i compile (AppDriver vs Class) ?
trusktr