转换不明确-C++

Ambiguous conversion - C++

本文关键字:-C++ 不明确 转换      更新时间:2023-10-16

我正试图使用C++和Xcode作为编译器来编写一个函数,以测试a是否为回文。当参数是"C++出生"类型(如int、long、double等)时,代码运行良好,但我希望将函数用于较大的值。所以我使用了BigInteger类型的参数。但是编译器在行上给出了一个错误

 BigInteger q = x - floor(x.toLong()/10)*10

表示CCD_ 1。这是整个代码:

#include <iostream>
#include "BigInteger.hh"
using namespace std;
bool isPalindrom(BigInteger x){
long ch = ceil(log10(x.toUnsignedLong())), n[ch];
//    cout << floor(log10(x)) + 1 << endl;
for (int i = 0; i <= ch; i++){
    BigInteger q = x - floor(x.toLong()/10)*10;
    n[i] = q.toInt();
    //        cout << n[i] << endl;
    x /= 10;
}
for (long i = 0; i <= ceil(ch); i++){
    if (n[i] != n[ch - i]){
        return false;
    }
}
return true;
}

我该如何解决这个问题?

如果要一直转换为long,那么使用BigInteger没有什么意义。

您可以只使用BigInteger操作来编写该内容,其方式与使用基元整数的方式完全相同:

bool isPalindrome(BigInteger x){
   std::vector<int> digits;
   while (x > 0)
   {
      digits.push_back((x % 10).toInt());
      x /= 10;
   }
   size_t sz = digits.size();
   for (size_t i = 0; i < sz; i++){
      if (digits[i] != digits[sz - i - 1]){
         return false;
      }
   }
   return true;
}

也许是

  BigInteger q (static_cast<long>(x - floor(x.toLong()/10)*10));

可能会让编译器更高兴。在BigInteger.hh中查找公共构造函数。注意,floor给出了一个double,因此子动作也给出了double,而BigInteger没有相应的构造函数。