“重载的FUNCTION的调用含糊不清”(转换构造函数C ++)

时间:2018-11-15 06:49:02

标签: c++ class constructor

我正在尝试在c ++ Point类中实现转换构造函数,并且收到错误消息:“对重载的FUNCTION的调用不明确”。这是什么意思?看我的讲师如何在她的程序中实现转换构造函数,我看不到我在做什么错。

这是头文件中的代码:

Point(int n);

这是类文件中的代码:

//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);

    }
}

这是驱动程序文件中的代码:

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

有什么主意我应该怎么解决?

编辑: -错误消息的完整输出:

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)

1 个答案:

答案 0 :(得分:2)

您的构造函数相互冲突:

var query = string.Empty;
var index=0;
foreach(var animal in animals) {
    if (query.Length>0) {
        query +=" and ";
    }
    var paramName = "@animalName" + index++;
    query +="(animals like " + paramName + " or animals like '%' + " + paramName + " or animals like " + paramName + " + '%' or animals like '%' + " + paramName + " + '%')";
    SqlParameter thisParam = new SqlParameter(paramName, animal);
    command.Parameters.Add(thisParam);
}
command.CommandText = "select * from tableName WHERE " + query;

并且编译器无法选择调用哪一个。 也许,一些分离会有所帮助:

BitConverter.GetBytes