gpt4 book ai didi

c++ - "Call of overloaded FUNCTION is ambiguous"(转换构造函数 C++)

转载 作者:行者123 更新时间:2023-11-28 01:27:01 27 4
gpt4 key购买 nike

我试图在我的 C++ Point 类中实现一个转换构造函数,但我收到错误消息“重载函数的调用不明确”。这是什么意思?查看我的讲师如何在她的程序中实现转换构造函数,我看不出我做错了什么。

这是头文件中的代码:

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)

最佳答案

你的构造函数相互冲突:

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

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

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

关于c++ - "Call of overloaded FUNCTION is ambiguous"(转换构造函数 C++),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53313870/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com