Sauromayne
Sauromayne

Reputation: 105

"Call of overloaded FUNCTION is ambiguous" (Conversion Constructor C++)

I'm trying to implement a conversion constructor in my c++ Point class and I am receiving the error "call of overloaded FUNCTION is ambiguous". What does this mean? Looking at how my instructor implemented the conversion constructor in her program I can't see what I'm doing wrong.

Here is the code in the header file:

Point(int n);

Here is the code in the class file:

//conversion constructor - initializes the data members from a 4 digit integer number 
//(yyxx).  If the number is less than 3 digits, set the data members to 0.
Point::Point(int n)
{
    int numDigits = 1;
    int temp = n; //temp variable to manipulate n while finding numDigits

    //while loop to find out how many digits
    while(temp/10 > 0)
    {
        numDigits++;
        temp = temp/10;
    }

    if(numDigits < 3)
    {
        x = 0;
        y = 0;
    }
    else if(numDigits == 3)
    {
        x = n%10;
        y = n/10;
    }
    else    //must be 4 digits
    {
        int y1 = n/10/10/10%10;
        int y2 = n/10/10%10;
        int x1 = n/10%10;
        int x2 = n%10;

        stringstream ss;
        ss << y1 << y2;
        string intStr1 = ss.str();

        ss << x1 << x2;
        string intStr2 = ss.str();

        x = std::stoi(intStr2);
        y = std::stoi(intStr1);

    }
}

and here is code in the driver file:

//conversion constructor
    Point x(123);
    Point y(1234);
    Point z(12);
    cout << "conversion constructor: 3 digits:           " << x.orderedPair() << endl;
    cout << "conversion constructor: 4 digits:           " << y.orderedPair() << endl;
    cout << "conversion constructor: less than 3 digits: " << z.orderedPair() << endl;

Any ideas what I should do to fix this?

Edit: -Full output of error message:

cd 'C:\Users\npods\Documents\CSC240\C\Point'
C:\cygwin64\bin\make.exe -f Makefile CONF=Debug
"/usr/bin/make" -f nbproject/Makefile-Debug.mk QMAKE= SUBPROJECTS= .build-conf
make[1]: Entering directory '/cygdrive/c/Users/npods/Documents/CSC240/C/Point'
"/usr/bin/make"  -f nbproject/Makefile-Debug.mk dist/Debug/Cygwin-Windows/point.exe
make[2]: Entering directory '/cygdrive/c/Users/npods/Documents/CSC240/C/Point'
mkdir -p build/Debug/Cygwin-Windows
rm -f "build/Debug/Cygwin-Windows/main.o.d"
g++    -c -g -MMD -MP -MF "build/Debug/Cygwin-Windows/main.o.d" -o build/Debug/Cygwin-Windows/main.o main.cpp
main.cpp: In function 'int main()':
main.cpp:23:16: error: call of overloaded 'Point(int)' is ambiguous
     Point x(123);
                ^
In file included from main.cpp:8:0:
point.h:25:5: note: candidate: Point::Point(int)
     Point(int n);            //conversion constructor
     ^~~~~
point.h:24:5: note: candidate: Point::Point(const Point&)
     Point(const Point& old); //copy constructor
     ^~~~~
point.h:22:5: note: candidate: Point::Point(int, int)
     Point(int x = 0, int y = 0); //default constructor &
     ^~~~~
main.cpp:24:17: error: call of overloaded 'Point(int)' is ambiguous
     Point y(1234);
                 ^
In file included from main.cpp:8:0:
point.h:25:5: note: candidate: Point::Point(int)
     Point(int n);            //conversion constructor
     ^~~~~
point.h:24:5: note: candidate: Point::Point(const Point&)
     Point(const Point& old); //copy constructor
     ^~~~~
point.h:22:5: note: candidate: Point::Point(int, int)
     Point(int x = 0, int y = 0); //default constructor &
     ^~~~~
main.cpp:25:15: error: call of overloaded 'Point(int)' is ambiguous
     Point z(12);
               ^
In file included from main.cpp:8:0:
point.h:25:5: note: candidate: Point::Point(int)
     Point(int n);            //conversion constructor
     ^~~~~
point.h:24:5: note: candidate: Point::Point(const Point&)
     Point(const Point& old); //copy constructor
     ^~~~~
point.h:22:5: note: candidate: Point::Point(int, int)
     Point(int x = 0, int y = 0); //default constructor &
     ^~~~~
make[2]: *** [nbproject/Makefile-Debug.mk:69: build/Debug/Cygwin-Windows/main.o] Error 1
make[2]: Leaving directory '/cygdrive/c/Users/npods/Documents/CSC240/C/Point'
make[1]: *** [nbproject/Makefile-Debug.mk:60: .build-conf] Error 2
make[1]: Leaving directory '/cygdrive/c/Users/npods/Documents/CSC240/C/Point'
make: *** [nbproject/Makefile-impl.mk:40: .build-impl] Error 2

BUILD FAILED (exit value 2, total time: 968ms)

Upvotes: 0

Views: 295

Answers (1)

snake_style
snake_style

Reputation: 1168

Your constructors conflict with each other:

Point::Point(int x = 0, int y = 0);
Point::Point(int n);

And the compiler cannot choose which one to call. Perhaps, some separation will help:

Point::Point(); // default
Point::Point(int x, int y);
Point::Point(int n);

Upvotes: 2

Related Questions