简体   繁体   中英

Overloading << operator unexpected behaviour?

I was writing bigInteger arithmetic's for my project as:

.hpp file:

#include<iostream>
  #include<vector>
  #include<string>
  #include<algorithm>

  using namespace std;

  class BigInteger{
    private:
      vector<int> digits;
    public:
      BigInteger(){}
      BigInteger(string input);
      BigInteger(int input);
      friend BigInteger operator+(BigInteger &lhs, BigInteger &rhs);
      BigInteger operator=(BigInteger &rhs);
      friend ostream &operator<<(ostream &out, BigInteger &t);
      vector<int> getDigits();
      int getNum(int t);
      void setDigits(vector<int> &vect);
      void handleCarry(BigInteger &t);
  };

.cpp file:

#include "BigInteger.hpp"

BigInteger::BigInteger(string input){
 int temp;·
 for(int i = input.size()-1; i >= 0; i--){
  temp = input[i] - '0';
  digits.push_back(temp);
 }
}

BigInteger::BigInteger(int input){
  int temp = input;
  while(temp){
    digits.push_back(temp%10);
    temp = temp/10;
  }
}

vector<int> BigInteger::getDigits(){
 return digits;
}

void BigInteger::setDigits(vector<int> &vect){
  digits = vect;
}

void BigInteger::handleCarry(BigInteger &t){
  vector<int> temp = t.getDigits();
  int len = temp.size();

  int num = 0;

  for(int i = 0; i < len; i++){
    carry = temp[i]/10;
    num = temp[i]%10;
    temp[i] = num;

    if((len -1) == i && carry > 0){
       temp.push_back(carry);
    }
    else
      temp[i+1] += carry;
  }
  t.setDigits(temp);
}

BigInteger operator+(BigInteger &lhs, BigInteger &rhs){
  int len = lhs.getDigits().size() > rhs.getDigits().size()? lhs.getDigits().size():rhs.getDigits().size();
  BigInteger result;
  vector<int> tmp;
  for(int i = 0; i < len; i++){
      tmp.push_back(lhs.getNum(i) + rhs.getNum(i));
  }
  result.setDigits(tmp);
  result.handleCarry(result);
  return result;
}

BigInteger BigInteger::operator=(BigInteger &rhs){
  vector<int> temp(rhs.getDigits());
  this->setDigits(temp);
  return *this;
}
ostream &operator<<(ostream &out, BigInteger &t){
  for(int i = t.getDigits().size()- 1; i >= 0; i--){
   out << t.getDigits()[i];
  }
 cout << endl;
  return out;
}

Main.cpp:

#include "BigInteger.hpp"

int main(){
BigInteger t("123");
BigInteger u(234);
BigInteger r = t + u;
cout << r << endl;
cout << t + u << endl;

return 0;
}

The desired result was to print the result in both the cases of cout statement in main() like:

 cout << t + u << endl;
 or 
 r = t + u;
 cout << r << endl;

But first statement is giving error saying no match for operator<< .

What is missing to attain the desired result?

t + u this returns a temporary that cannot bind to an lvalue-reference asked for by operator<<(ostream &out, BigInteger &t) , you need to add const :

ostream &operator<<(ostream &out, const BigInteger &t)

This should be the default for the stream operator anyway, since you shouldn't want to change the state of this object.

Your output operator should take a const reference:

ostream &operator<<(ostream &out,const BigInteger &t)
                                   ^^

You cant take a non-const reference to t+u , because it is a r-value, but you can bind a const-ref to a r-value.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM