簡體   English   中英

C ++ biginteger類運算符=

[英]C++ biginteger class operator=

我正在嘗試實現biginteger類,並在創建了具有適當頭文件的biginteger類之后,首先,我試圖定義一個operator =()運算符,因此當我創建一個新的biginteger對象時,能夠使其等於整數。

這是main.cpp:

#include <iostream>
#include "bigint.h"

using namespace std;

int main()
{
    bigint bela = 15;
    cout << "Hello world!" << bela.mennyi() <<endl;
    return 0;
}

這是biginteger標頭:

    #ifndef BIGINT_H
#define BIGINT_H

#include <vector>
#include <iostream>

class bigint
{
    public:
        bigint();
        void operator=(const int &a);
        int mennyi();

    protected:
    private:
        std::vector<int> numarray;
};

#endif // BIGINT_H

以及biginteger.cpp文件:

#include "bigint.h"
#include <iostream>

using namespace std;


bigint::bigint()
{
    numarray.resize(0);
}

void bigint::operator=(const int &a)
{
    int b = a;
    if(b >= 0)
    {
        numarray.resize(0);
        while(b!=0){
        numarray.push_back(b%10);
            b = b/10;
        }
    }
}

int bigint::mennyi()
{
    int ki = 0;
    for(int i = (numarray.size())-1; i>=0; i--)
    {
        ki = ki*10 + numarray[i];
    }
    return ki;
    }

當我開始調試時,我收到一條錯誤消息:請求從'int'轉換為非標量類型'bigint'。

您應該實現此構造函數:

bigint::bigint(int);

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM