C++ 大整数类运算符=

标签 c++ class

我正在尝试实现一个 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;
}

这是双整数 header :

    #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);

关于C++ 大整数类运算符=,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26022741/

相关文章:

c++ - 用灵气解析C风格的关系运算符

c++ - 从 0 到 255 的无限循环与无符号字符计数器

java - 如果缺少某些日期,如何反序列化?

java - 使用重载但方法不返回值。有任何想法吗?

typescript - 使用 typescript 在构造函数中动态设置类属性

c++ - COM:如何获取有关 COM 错误的更多详细信息?

c++ - 松弛的内存顺序会导致这里无限循环吗?

c++ - 使用除法技术求数组的最大公约数

C++ 虚拟继承、类可见性?

class - 在同一类构造函数中初始化静态std::vector <class>的问题