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