2014-04-13 106 views
1

所以,我對操作符重載非常陌生,無法獲取operator-,operator>和operator <的語法。重載operator-,operator <和operator>

我被授予了我的代碼的操作符+代碼,並且我很難將其更改爲操作符函數。我也爲我的運營商<函數獲取了很多錯誤,但不是我的operator>函數,它基本相同。

如果你可以闡明如何做到這一點和/或爲我提供更多幫助的鏈接,那將不勝感激。

這是我到目前爲止有:

(正如你看到的,我只複製和粘貼操作+到我的操作符 - 嘗試和理由吧)

BigInt.cpp

#include <iostream> 
#include <string> 
#include "BigInt.h" 

using namespace std; 

BigInt BigInt::operator+(BigInt operand) 
{ 
    int carry = 0; 
    int sum; 
    BigInt result; 
    list<int>::reverse_iterator rit1 = number.rbegin(); 
    list<int>::reverse_iterator rit2 = operand.number.rbegin(); 
    while ((rit1 != number.rend()) || (rit2 != operand.number.rend())) 
    { 
     sum = 0; 

     if (rit1 != number.rend()) 
     { 
      sum += *rit1; 
      rit1++; 
     } 

     if (rit2 != operand.number.rend()) 
     { 
      sum += *rit2; 
      rit2++; 
     } 

     sum += carry; 
     result.number.push_front(sum % 10); 
     carry = sum/10; 
    } 

    if (carry > 0) 
     result.number.push_front(carry); 

    return result; 
} 

void BigInt::display(ostream & out) 
{ 
    for (list<int>::iterator it = number.begin(); it != number.end(); it++) 
    { 
     cout << *it; 
    } 

    cout << endl; 
} 


void BigInt::read(istream & in) 
{ 
    string input; 

    cin >> input; 

    for (int i = 0; i < input.length(); ++i) 
    { 
      int x = input.at(i); 
      int y = char(x) - char('0'); 
      number.push_back(y); 
    } 



} 

bool BigInt::operator<(BigInt operand) 
{ 
    list<int>::reverse_iterator rit1 = number.rbegin(); 
    list<int>::reverse_iterator rit2 = operand.number.rbegin(); 
    int count1 = 0, count2 = 0; 
    bool check; 
    while ((rit1 != number.rend()) || (rit2 != operand.number.rend())) 
    { 
     if (rit1 > rit2) 
     { 
      count1++; 
      rit1++; 
      rit2++; 
     } 
     else if (rit2 > rit1) 
     { 
      count2++; 
      rit1++; 
      rit2++; 
     } 
     else 
     { 
      rit1++; 
      rit2++; 
     } 

    } 
    if(count1 < count2) 
    { 
     check = true; 
    } 
    else 
    { 
     check = false; 
    } 

    return check; 
} 

bool BigInt::operator>(BigInt operand) 
{ 
    list<int>::reverse_iterator rit1 = number.rbegin(); 
    list<int>::reverse_iterator rit2 = operand.number.rbegin(); 
    int count1 = 0, count2 = 0; 
    while ((rit1 != number.rend()) || (rit2 != operand.number.rend())) 
    { 
     if (rit1 > rit2) 
     { 
      count1++; 
      rit1++; 
      rit2++; 
     } 
     else if (rit2 > rit1) 
     { 
      count2++; 
      rit1++; 
      rit2++; 
     } 
     else 
     { 
      rit1++; 
      rit2++; 
     } 

    } 
    if(count1 > count2) 
    { 
     return true; 
    } 
    else 
    { 
     return false; 
    } 


} 

bool BigInt::operator==(BigInt operand) 
{ 
    list<int>::reverse_iterator rit1 = number.rbegin(); 
    list<int>::reverse_iterator rit2 = operand.number.rbegin(); 


    do 
    { 
     rit1++; 
     rit2++; 
    } 
    while(rit1 == rit2); 

    return false; 

} 

BigInt BigInt::operator-(BigInt operand) 
{ 
    int carry = 0; 
    int sum; 
    BigInt result; 
    list<int>::reverse_iterator rit1 = number.rbegin(); 
    list<int>::reverse_iterator rit2 = operand.number.rbegin(); 
    while ((rit1 != number.rend()) || (rit2 != operand.number.rend())) 
    { 
     sum = 0; 

     if (rit1 != number.rend()) 
     { 
      sum += *rit1; 
      rit1++; 
     } 

     if (rit2 != operand.number.rend()) 
     { 
      sum += *rit2; 
      rit2++; 
     } 

     sum += carry; 
     result.number.push_front(sum % 10); 
     carry = sum/10; 
    } 

    if (carry > 0) 
     result.number.push_front(carry); 

    return result; 
} 

BigInt.h

#include <list> 
using namespace std; 

class BigInt 
{ 
public: 
BigInt() {}; 
void read(istream & in); 
void display(ostream & out); 
BigInt operator+(BigInt operand); //overloading operator 
BigInt operator-(BigInt operand); //subtraction 
bool operator<(BigInt operand); 
bool operator>(BigInt operand); 
bool operator==(BigInt operand); 
list<int> number; //using STL list 
}; 

我沒有在我的main.cpp呢。

希望你能幫助我!謝謝!

編輯!!!:

忘記列出我的錯誤,我的操作<功能:

Error 10 error C2676: binary '<' : 'std::_List_iterator<_Mylist>' does not define this operator or a conversion to a type acceptable to the predefined operator c:\program files (x86)\microsoft visual studio 11.0\vc\include\xutility 1014 
Error 4 error C2784: 'bool std::operator <(const _Elem *,const std::basic_string<_Elem,_Traits,_Alloc> &)' : could not deduce template argument for 'const _Elem *' from 'std::_List_iterator<_Mylist>' c:\program files (x86)\microsoft visual studio 11.0\vc\include\xutility 1014 
Error 8 error C2784: 'bool std::operator <(const std::_Revranit<_RanIt,_Base> &,const std::_Revranit<_RanIt2,_Base2> &)' : could not deduce template argument for 'const std::_Revranit<_RanIt,_Base> &' from 'std::_List_iterator<_Mylist>' c:\program files (x86)\microsoft visual studio 11.0\vc\include\xutility 1014 
Error 3 error C2784: 'bool std::operator <(const std::basic_string<_Elem,_Traits,_Alloc> &,const _Elem *)' : could not deduce template argument for 'const std::basic_string<_Elem,_Traits,_Alloc> &' from 'std::_List_iterator<_Mylist>' c:\program files (x86)\microsoft visual studio 11.0\vc\include\xutility 1014 
Error 5 error C2784: 'bool std::operator <(const std::basic_string<_Elem,_Traits,_Alloc> &,const std::basic_string<_Elem,_Traits,_Alloc> &)' : could not deduce template argument for 'const std::basic_string<_Elem,_Traits,_Alloc> &' from 'std::_List_iterator<_Mylist>' c:\program files (x86)\microsoft visual studio 11.0\vc\include\xutility 1014 
Error 2 error C2784: 'bool std::operator <(const std::list<_Ty,_Alloc> &,const std::list<_Ty,_Alloc> &)' : could not deduce template argument for 'const std::list<_Ty,_Alloc> &' from 'std::_List_iterator<_Mylist>' c:\program files (x86)\microsoft visual studio 11.0\vc\include\xutility 1014 
Error 6 error C2784: 'bool std::operator <(const std::move_iterator<_RanIt> &,const std::move_iterator<_RanIt2> &)' : could not deduce template argument for 'const std::move_iterator<_RanIt> &' from 'std::_List_iterator<_Mylist>' c:\program files (x86)\microsoft visual studio 11.0\vc\include\xutility 1014 
Error 9 error C2784: 'bool std::operator <(const std::pair<_Ty1,_Ty2> &,const std::pair<_Ty1,_Ty2> &)' : could not deduce template argument for 'const std::pair<_Ty1,_Ty2> &' from 'std::_List_iterator<_Mylist>' c:\program files (x86)\microsoft visual studio 11.0\vc\include\xutility 1014 
Error 7 error C2784: 'bool std::operator <(const std::reverse_iterator<_RanIt> &,const std::reverse_iterator<_RanIt2> &)' : could not deduce template argument for 'const std::reverse_iterator<_RanIt> &' from 'std::_List_iterator<_Mylist>' c:\program files (x86)\microsoft visual studio 11.0\vc\include\xutility 1014 
+1

目前尚不清楚問題出在哪裏。如果你知道如何實現'operator +',那麼實現'operator-'的難度是多少? –

+0

我會從''while'開始將'||'改爲'&&'。如果這符合我的想法,那麼邏輯運算符在寫入時就不正確。 – WhozCraig

+0

我很困惑如何修改操作員+操作員。運營商和運營商之間是否有很少的修改? – Midge

回答

2

if (rit1 > rit2)if (*rit1 > *rit2),你要比較的迭代器不是迭代器自理的內容。

+0

這解決了我最大的錯誤。謝謝。 – Midge