bigint运算符重载

时间:2013-06-05 14:28:53

标签: c++ oop operator-overloading bigint

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

错误:对'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

1 个答案:

答案 0 :(得分:4)

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

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

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