gpt4 book ai didi

c++ - bigint 运算符重载

转载 作者:行者123 更新时间:2023-11-30 01:58:45 25 4
gpt4 key购买 nike

问题描述:我正在尝试使用运算符重载创建一个大整数类,我相信到目前为止一切顺利,但是当我尝试编译时我不断收到此错误。知道问题可能是什么吗?它不会给我输入错误,只有输出错误。

错误:未定义对 `bigint::tostring() const' 的引用

#ifndef HEADER_H_INCLUDED
#define HEADER_H_INCLUDED


using namespace std;

class bigint{

public:
bigint(); //default constructor - set this to zero
bigint(int x0);
bigint(int x0, int x1);
bigint(int x0, int x1, int x2);
bigint(int x0, int x1, int x2, int x3);
bigint(int x0, int x1, int x2, int x3, int x4);
string tostring() const;


private:
int v[5];
};

ostream& operator <<(ostream & out, const bigint outpt){
out << outpt.tostring();
return out;
}


istream& operator >>(istream & in, const bigint& inpt){
return in;
} //need to fix this


bigint & operator +(const bigint & ls, const bigint & rs) {
return bigint(ls) + rs;
}//addition operator

bigint & operator -(const bigint & ls, const bigint & rs){
return bigint(ls) - rs;
} //subtraction operator

bool operator <(const bigint & ls, const bigint rs){
return bigint(ls) < rs;
} //use bool because these values can only be true or false

bool operator >(const bigint & ls, const bigint rs){
return bigint(ls) > rs;
}

bool operator >=(const bigint & ls, const bigint rs){
return bigint(ls) >= rs;
}

bool operator <=(const bigint & ls, const bigint rs){
return bigint(ls) <= rs;
}

bool operator ==(const bigint & ls, const bigint rs){
return bigint(ls) == rs;
}

bool operator !=(const bigint & ls, const bigint rs){
return bigint(ls) != rs;
}


#endif // HEADER_H_INCLUDED

最佳答案

我没有看到您对 tostring() 的实现进行了编码。您必须编写自己的 tostring() 实现。

该函数将获取数字,将其转换为字符串并返回该字符串。您可以使用 streamitoasprintf

如果其他地方有任何本地 tostring() 方法,请检查“string”的 S 肯定是大写而不是小写 (toString())。

关于c++ - bigint 运算符重载,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16942618/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com