简体   繁体   English

赋值运算符不起作用

[英]assignment operator doesnt work

I have a constructor to represent big numbers and store them into a vector: my class is composed of a vector, a boolean and an int and is supposed to represent big numbers. 我有一个表示大数的构造函数,并将它们存储到一个向量中:我的类由一个向量,一个布尔值和一个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;
        }
    }       
}

and I get an error: http://pastebin.com/cy82XaLF 我收到一个错误: http : //pastebin.com/cy82XaLF

A conversion sequence can only contain at most one implicit user-defined conversion. 转换序列最多只能包含一个隐式用户定义的转换。 Yours contains two: const char[] to string , then string to CBigInt . 您的包含两个: const char[]string ,然后stringCBigInt

A simple fix is to make one conversion explicit: 一个简单的解决方法是使一次转换明确:

CBigInt autre("123456789012345678901111");

在已经拥有的构造函数之上声明并实现一个附加的构造函数:

CBigInt::CBigInt(const char* str):CBigInt(string(str)){}

Probabily they doesn't work because the compile do not provide two implicit convertion at same operation, one from Const Char* to String , and String to CBigInt . 可能它们不起作用,因为在同一操作下编译不会提供两次隐式转换,一次是从Const Char*String ,另一次是从StringCBigInt And only when the type was a CBigInt is possible call operator = . 而且只有当类型为CBigInt时,才可以调用operator =

This could be repare at this peace of code that compile correctily with your class: 这可以与您的班级正确编译的代码的和平相处:

   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

My sugestion is copy your code from CBigInt(string) to CBigInt(const char*) and adpatad to looking for '\\0' at the end of string. 我的建议是将您的代码从CBigInt(string)复制到CBigInt(const char*)并适应于在字符串末尾查找“ \\ 0”。

Another suggestion that I have to you is exchange your signature from CBigInt(string) to CBigInt(const string&) , it's faster because doesn't copy the string and no code have to be changed. 我对您的另一个建议是将您的签名从CBigInt(string)交换为CBigInt(const string&) ,它的速度更快,因为不需要复制字符串,也无需更改代码。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM