【问题标题】:BigInt operator+ function? [closed]BigInt 运算符+函数? [关闭]
【发布时间】:2013-02-03 23:32:15
【问题描述】:

我一直在尝试使用 operator+ 功能,但它似乎不起作用.. 不知道出了什么问题:s

BigInt.h

#include <iostream>
using namespace std;
using std::cout;

#ifndef BIGINT_H
#define BIGINT_H

class BigInt
{
//input and output operators
   friend istream & operator >> (istream &, BigInt &);
   friend ostream & operator << (ostream &, const BigInt &);

//overloaded operators
   BigInt operator + (BigInt &, BigInt &);

public:
   BigInt(); //default constructor
   BigInt(int); //initializes array with user-specified numbers

  // BigInt operator +(const BigInt &); //???

   void display(); //prints array

private:
   static const int CAPACITY = 40;
   int Digits[CAPACITY]; //stores all digits

};
#endif

BigInt.cpp

#include <iostream>
#include "BigInt.h"
using std::cout;

BigInt::BigInt()
{
   for (int i = 0; i < CAPACITY; i++)
      Digits[i] = 0;
}

BigInt::BigInt(int InitNum)
{
   for(int i = 0; i < CAPACITY; ++i)
      Digits[i] = 0;

   for (int i = 0; InitNum != 0 ; ++i)
   {
      Digits[i] = (InitNum % 10);
      InitNum = InitNum / 10;
   }
}

//------------------------------------------------------------------
BigInt operator +(BigInt & lhs, BigInt & rhs)
{
   BigInt results;
   int carry = 0;

   for (int i = 39 ; i >= 0; i--)
   {
      int indexAddition = lhs.Digits[i] + rhs.Digits[i] + carry;
      if (indexAddition > 9)
         carry = 1;
      else
         carry = 0;

      results.Digits[i] %= 10;
   }

   return results;
}

//-----------------------------------------------------------------
bool BigInt::operator == (const BigInt & rhs)
{
   for(int i = 0; i < CAPACITY; i++)
   {
      if (Digits[i] != rhs.Digits[i])
         return false;
   }
   return true;
}

//-----------------------------------------------------------------
ostream & operator << (ostream & cout, const BigInt& a)
{
   for(int i = a.CAPACITY - 1; i >= 0 ; i--)
      cout << a.Digits[i];

   cout << "\n";

   return cout;
}

istream & operator >> (istream & cin,  BigInt& a)
{
   for(int i = 0; i < a.CAPACITY; i++)
      cin >> a.Digits[i];

   return cin;
}

//---------------------------------------------------------------
void BigInt::display()
{
   for(int i = CAPACITY - 1; i >= 0; i--)
      cout << Digits[i];

   cout << "\n";
}

Main.cpp

#include <iostream>
#include <cstdlib>
#include <fstream>
#include "BigInt.h"

int main()
{
   BigInt object1(45756369);
   BigInt object2(47435892);

   cout << object1;
   object2.display();

   BigInt object3 = object1 + object2;

   cout << object3;

   return 0;
}

当我尝试使用时

 BigInt operator +(const BigInt &); 

,就是这样实现的:

BigInt::operator +(const BigInt &a)
{
   BigInt result;

   for(int i = 0; i < CAPACITY; i++)
      result = Digits[i] + a.Digits[i];

   return result;
}

我会收到这个错误:

BigInt.cpp: In function âBigInt& operator+(const BigInt&)â:
BigInt.cpp:36:23: error: âCAPACITYâ was not declared in this scope
BigInt.cpp:37:16: error: âDigitsâ was not declared in this scope
BigInt.h:50:23: error: âint BigInt::Digits [40]â is private
BigInt.cpp:37:30: error: within this context
BigInt.cpp:34:11: warning: reference to local variable âresultâ returned [enabled by default]

当我使用

BigInt operator +(BigInt & lhs, BigInt & rhs)
{
   BigInt results;
   int carry = 0;

   for (int i = 39 ; i >= 0; i--)
   {
      int indexAddition = lhs.Digits[i] + rhs.Digits[i] + carry;

      if (indexAddition > 9)
         carry = 1;
      else
         carry = 0;

      results.Digits[i] %= 10;
   }

   return results;
}


In file included from BigInt.cpp:9:0:
BigInt.h:31:39: error: âBigInt BigInt::operator+(BigInt&, BigInt&)â must take either zero or one     argument
BigInt.h: In function âBigInt operator+(BigInt&, BigInt&)â:
BigInt.h:50:23: error: âint BigInt::Digits [40]â is private
BigInt.cpp:50:27: error: within this context
BigInt.h:50:23: error: âint BigInt::Digits [40]â is private
BigInt.cpp:50:43: error: within this context
BigInt.h:50:23: error: âint BigInt::Digits [40]â is private
BigInt.cpp:57:11: error: within this context

为什么? 谢谢!

【问题讨论】:

  • 请不要发布代码转储。它们对未来的访问者根本没有帮助。
  • 但是当参数为not const时它可以工作,对吗?
  • 它编译,但它不添加...
  • 这如何编译?我在第一个代码块中遇到错误...
  • 我编辑的代码,一点这个BigInt BigInt::operator + (BigInt &amp; rhs) { BigInt results; for(int i = CAPACITY - 1; i &gt;= 0; i--) results = Digits[i] + rhs.Digits[i]; return results; }它给了我这样的:0000000000000000000000000000000045756369 0000000000000000000000000000000047435892 0000000000000000000000000000000000000011 SPAN>

标签: c++ bigint


【解决方案1】:

operator+ 函数不是BigInt 的成员,因此它无法访问其中的私有成员。您应该将其声明为类的朋友:

friend BigIn operator +(BigInt &, BigInt &);

【讨论】:

  • 好吧.. 我做到了,现在我收到了BigInt.cpp:45:9: warning: reference to local variable âresultsâ returned [enabled by default]
  • @TongZhang 错误信息非常直接。您通过引用返回一个危险的局部变量,因为该变量不会超过函数调用。为什么不按值返回呢?
  • 是的,我做到了。可以了,谢谢!
【解决方案2】:

您忘记了

第 4 行中的 results.Digits[i]
BigInt BigInt::operator+(const BigInt &a)
{
   BigInt result;

   for(int i = 0; i < CAPACITY; i++)
      result.Digits[i] = Digits[i] + a.Digits[i];

   return result;
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-05-05
    • 2014-03-30
    • 2017-08-07
    相关资源
    最近更新 更多