我创建了自己的四种方法来将字符串处理为数字:
std::string addStrings(std::string,std::string);
std::string subtractStrings(std::string,std::string);
std::string multiplyStrings(std::string,std::string);
std::string divideStrings(std::string,std::string);
然后我决定创建大数的类(称为bin)。我对复制构造函数和复制赋值运算符有点陌生,所以我需要你的帮助来修复我的代码:
class bin{
private:
std::string value;
public:
bin(){}
bin(const char* v1){
value = v1;
}
bin(std::string v1){
value = v1;
}
bin(const bin& other){
value = other.value;
}
bin& operator=(const bin& other){
value = other.value;
return *this;
}
bin& operator=(const char* v1){
value = v1;
return *this;
}
std::string getValue() const{
return value;
}
friend std::ostream& operator<<(std::ostream&,bin&);
};
std::ostream& operator<<(std::ostream& out,bin& v){
out << v.value;
return out;
}
bin operator+(bin& value1,bin& value2){
return bin(addStrings(value1.getValue(),value2.getValue()));
}
bin operator-(bin& value1,bin& value2){
return bin(subtractStrings(value1.getValue(),value2.getValue()));
}
bin operator*(bin& value1,bin& value2){
return bin(multiplyStrings(value1.getValue(),value2.getValue()));
}
bin operator/(bin& value1,bin& value2){
return bin(divideStrings(value1.getValue(),value2.getValue()));
}
为什么这样做有效:
bin d = a/c;
std::cout << d << std::endl;
但这并不是:
标准::cout<&书信电报;a/c;
(a和c较早宣布)。此外,运算符链接也不起作用,例如:
bin d = a * b + d;
投掷:
no match for operator* (operands are bin and bin).
非常感谢。