0

私は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

なんで?ありがとうございました!

4

2 に答える 2

1

結果を忘れました。4行目の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;
}
于 2013-02-03T23:59:09.807 に答える
1

そのoperator+関数はのメンバーではないBigIntため、そのプライベートメンバーにアクセスすることはできません。代わりに、クラスの友達として宣言する必要があります。

friend BigIn operator +(BigInt &, BigInt &);
于 2013-02-03T23:53:51.040 に答える