我正在上课CBigInt。 CBigInt类是具有无限长度的整数类。它看起来像这样:
class CBigInt{
public:
string number;
int sign;
CBigInt()
CBigInt(const int a);
CBigInt(const string a);
CBigInt& operator = (const string rhs);
CBigInt& operator=(const CBigInt& rhs);
CBigInt& operator=(int rhs );
CBigInt& operator+=(const CBigInt& rhs);
CBigInt operator+(const CBigInt& rhs);
CBigInt& operator+=(const string rhs);
CBigInt operator+(const string rhs);
CBigInt& operator *=(const CBigInt& rhs);
CBigInt operator*(const CBigInt& rhs);
CBigInt& operator*=(const string rhs);
CBigInt operator*(const string rhs);
friend ostream& operator<<(ostream& off, const CBigInt& big);
friend istream& operator>>(istream& in, CBigInt& big );
}
所有操作符都已定义,并且可以找到我能想到的所有示例,除了这一个:
CBigInt a, b;
a *= 0;
编译器无法决定使用两种功能中的哪一种:
CBigInt& operator *=(const CBigInt& rhs);
or
CBigInt& operator *=(const string rhs);
我读了几个非常相似的问题,但它没有多大帮助。
错误:
/home/michal/Desktop/prog/big/main.cpp||In function ‘int main()’:|
/home/michal/Desktop/prog/big/main.cpp|345|error: ambiguous overload for ‘operator*=’ in ‘a *= 0’|
/home/michal/Desktop/prog/big/main.cpp|345|note: candidates are:|
/home/michal/Desktop/prog/big/main.cpp|198|note: CBigInt& CBigInt::operator*=(const CBigInt&)|
/home/michal/Desktop/prog/big/main.cpp|247|note: CBigInt& CBigInt::operator*=(std::string)|
||=== Build finished: 4 errors, 0 warnings ===|
答案 0 :(得分:3)
只需删除此运算符
即可CBigInt operator*(const string rhs);
操作员
就足够了CBigInt operator*(const CBigInt& rhs);
因为该类有两个转换构造函数
CBigInt(const int a);
CBigInt(const string a);
所以不是运算符集
CBigInt& operator = (const string rhs);
CBigInt& operator=(const CBigInt& rhs);
CBigInt& operator=(int rhs );
CBigInt& operator+=(const CBigInt& rhs);
CBigInt operator+(const CBigInt& rhs);
CBigInt& operator+=(const string rhs);
CBigInt operator+(const string rhs);
CBigInt& operator *=(const CBigInt& rhs);
CBigInt operator*(const CBigInt& rhs);
CBigInt& operator*=(const string rhs);
CBigInt operator*(const string rhs);
你可以写
CBigInt& operator=(const CBigInt& rhs);
CBigInt& operator+=(const CBigInt& rhs);
CBigInt operator+(const CBigInt& rhs);
CBigInt& operator *=(const CBigInt& rhs);
CBigInt operator*(const CBigInt& rhs);
还要从
更改构造函数CBigInt(const int a);
CBigInt(const string a);
到
CBigInt( int a);
CBigInt(const string &a);
考虑到代码包含拼写错误。你错过了分号
CBigInt()
和
类定义的右大括号。
例如,此代码已成功编译
#include <iostream>
#include <string>
class CBigInt{
public:
std::string number;
int sign;
CBigInt() {}
CBigInt(const int a) {}
CBigInt(const std::string &a) {}
CBigInt& operator=(const CBigInt& rhs) { return *this; }
CBigInt& operator+=(const CBigInt& rhs);
CBigInt operator+(const CBigInt& rhs);
CBigInt& operator *=(const CBigInt& rhs) { return *this; }
CBigInt operator*(const CBigInt& rhs);
friend std::ostream& operator<<(std::ostream& off, const CBigInt& big);
friend std::istream& operator>>(std::istream& in, CBigInt& big );
};
int main()
{
CBigInt i1, i2, i3;
i1 = i2 *= i3;
i1 *= 0;
i1 *= std::string( "Hello" );
return 0;
}
如果您需要使用字符串文字,那么您可以添加重载的运算符
CBigInt& operator *=(const char *rhs) { return *this; }
答案 1 :(得分:0)
轻松修复是a *= CBigInt(0);
。
答案 2 :(得分:0)
由于没有operator*=
函数具有类型int
作为参数类型,因此编译器必须决定可以使用哪些其他函数。它发现int
可以转换为CBigInt
和string
。编译器无法解决这种歧义。因此错误。您可以通过明确解决这个问题。我想你想用
a *= CBigInt(0);
答案 3 :(得分:0)
简单的解决方案是明确标记转化运算符:
explicit CBigInt(const std::string &a) {}
这将阻止转换被认为是运算符* =。
的重载解析如果您定义转换运算符和可能使用该转换运算符的重载,那么您将遇到不好的时间。通常,应该避免隐式转换,除非它们既非常必要又非常简单。
使用内置类型系统作为指南。如果C ++标准需要在等效类型之间进行显式转换(在int和char *之间),则转换运算符不应该是隐式的。