我有一个构造函数来表示大数字并将它们存储到一个向量中: 我的类由向量,布尔值和int组成,应该代表大数字。
CBigInt(const char* str2)
{
string str=string(str2);
num.resize(200000);
if (str[0]=='-')
{
signe=true;
pos=str.length()-1;
for (int i=pos;i>0;i--)
{
num[pos-i]=str[i]-48;
}
}
else
{
signe=false;
pos=str.length();
for (int i=pos;i>=0;i--)
{
num[pos-i-1]=str[i]-48;
}
}
}
答案 0 :(得分:3)
转换序列最多只能包含一个隐式的用户定义转换。您的自己包含两个:const char[]
到string
,然后是string
到CBigInt
。
一个简单的解决方法是使一个转换显式:
CBigInt autre("123456789012345678901111");
答案 1 :(得分:3)
在你已经拥有的构造函数之上声明并实现一个额外的构造函数:
CBigInt::CBigInt(const char* str):CBigInt(string(str)){}
答案 2 :(得分:0)
可能它们不起作用,因为编译不会在同一操作中提供两个隐式转换,一个从Const Char*
到String
,String
到CBigInt
。只有当类型为CBigInt
时才可以调用operator =
。
这可以用这种与你的班级正确编译的代码的平衡来重现:
CBigInt a("99") //justo one implicit convertion, to const char * to string
string std;
a = std; //just one implicit convertion, to String to CBigInt
a = string("99"); //just one implicit convertion, to String to CBigInt
我的提法是将您的代码从CBigInt(string)
复制到CBigInt(const char*)
,并将adpatad复制到字符串末尾寻找'\ 0'。
我要求您提出的另一个建议是将您的签名从CBigInt(string)
更改为CBigInt(const string&)
,速度更快,因为不会复制字符串,也不需要更改代码。