【问题标题】:bigint Operator Overloadingbigint 运算符重载
【发布时间】:2013-06-05 14:28:52
【问题描述】:

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

错误:对 `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() 的实现。
  • 也许您可以提供一个出现问题的最小工作示例,删除您不需要的所有内容(例如未使用的构造函数、未使用的运算符等...)。
  • 未定义的引用是链接器错误。这不是编译错误。您没有在错误消息中定义事物,忘记链接定义它的文件,忘记链接到定义它的库,或者,如果它是静态库,则链接器命令行上的顺序错误.检查哪一个。 (请注意,一些链接器将其称为未解析的外部)
  • 你的 rhs args 错过了一半 &

标签: c++ oop operator-overloading bigint


【解决方案1】:

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

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

如果其他地方有任何原生 tostring() 方法,请检查“字符串”的 S 是否肯定是大写而不是小写(toString())。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-05-05
    • 2019-07-03
    • 1970-01-01
    • 1970-01-01
    • 2016-02-19
    相关资源
    最近更新 更多