2012-04-28 65 views
0

我需要做一些邏輯比較並返回一個布爾值答案。邏輯比較==運算符過載

下面是從.cpp文件的代碼:

bool MyString::operator==(const MyString& other)const 
{ 
    if(other.Size == this.Size) 
    { 
      for(int i = 0; i < this.Size+1; i++) 
      { 
        if(this[i] == other[i]) 

          return true; 
      } 
    } 
    else 
      return false; 
} 

這裏是從的main.cpp文件名爲:

if (String1 == String4) 
{ 
    String3.Print(); 
} 
else 
{ 
    String4.Print(); 
} 

這裏是有編譯錯誤,我得到:

error: request for member `Size` in `this`, which is of non-class type `const MyString* const` 
error: no match for `operator[]` in `other[i]` 
+2

'如果(這[一] ==等[1]) 迴歸真實;'這將在以後導致你的問題。想想你在那裏做什麼。 – chris 2012-04-28 22:59:39

+0

這實際上是我現在得到的唯一錯誤。我想要做的就是比較兩個字符串的內容。我怎麼可能做到這一點,而不必重載[]運算符呢? – user1363061 2012-04-29 00:01:10

回答

2

問題與您的代碼:

  • this[i]:你顯然想在這裏訪問字符串的第i個字符。這不是那樣做的。假設你的班級超載operator[],你需要(*this)[i]。或者,您可以直接訪問字符串的內部表示。

  • if(this[i] == other[i]) return true;:想一想,比較字符串「A1」和「AB」是什麼意思。

  • for() {...}:退出循環時會發生什麼?如果比較設法通過循環而不返回,則需要返回一些內容。

+0

該類實際上並沒有重載operator [],在這種情況下,我應該怎麼做?對於這個循環,我只想檢查兩個字符串的長度是否相同,內容是否相同。如果是這樣,則返回true。 – user1363061 2012-04-28 23:55:01

+0

您可以訪問字符串的內部表示。你當然有一些數據成員存儲字符串的內容。所以訪問它。 – 2012-04-29 00:33:08

+0

這就是我很困惑,需要幫助。 – user1363061 2012-04-29 00:35:59

4

this是一個指針,因此您必須對其進行解引用:

this->Size; 

而且我覺得你operator==的這個邏輯是有缺陷的 - 在這裏,它返回true如果任何字符等於字符在第二個字符串相同的位置。你的循環更改爲

 for(int i = 0; i < this->Size+1; i++) 
     { 
       if(this[i] != other[i]) 

         return false; 
     } 

,並把return true;,而不是你的代碼(else條款)的最後部分的比較整個字符串。

正如賽斯所說,你不能operator[]this使用如上 - 這樣它當作陣列(即this[i]真的*(this + i) - 所以沒有什麼是你在想它是)。改爲訪問您的內部存儲成員。

+0

你告訴他不要使用'this.Size',那麼你在你的例子中使用它?此外,'(* this)[i]'是一個完全合理的選項(如果'operator []'做比索引數組更復雜的事情)。我不認爲循環終止條件是正確的。 – 2012-04-28 23:08:21

+1

甚至更​​好,使用'std :: equal'。 – juanchopanza 2012-04-28 23:09:08

+0

@Griwes:我知道,但是你的代碼片段沒有。 – 2012-04-29 14:14:12

0

您還沒有指定是否可以使用C++標準算法。 在這裏,你已經說明了兩個版本,使用手寫循環和std::equal算法:

//#define USE_STD_ALGORITHM 1 // uncomment to enable std::equal version 
#include <cassert> 
#include <algorithm> 
#include <stdexcept> 

// NOTE: partial simplest definition for the test and presentation purposes only. 
struct MyString 
{ 
    MyString(char const* s, std::size_t size) : data(s), Size(size) {} 
    char const& operator[](std::size_t index) const; 
    bool operator==(const MyString& other) const; 
private: 
    char const* data; 
    std::size_t Size; 
}; 

char const& MyString::operator[](std::size_t index) const 
{ 
    if (index < Size) 
     return data[index]; 
    throw std::out_of_range("index invalid"); 
} 

bool MyString::operator==(const MyString& other) const 
{ 
    if (this->Size == other.Size) 
    { 
#ifdef USE_STD_ALGORITHM 
     return std::equal(data, data+Size, other.data); 
#else 
    bool equal = true; 
    for(std::size_t i = 0; i < this->Size; ++i) 
    { 
     if((*this)[i] != other[i]) 
     { 
      equal = false; 
      break; 
     } 
    } 
    return equal; 
#endif 
    } 

    return false; 
} 

int main() 
{ 
    char const* a = "abc"; 
    char const* b = "abc"; 
    MyString sa(a, 3); 
    MyString sb(b, 3); 
    assert(sa == sb); 

    char const* c = "adc"; 
    MyString sc(c, 3); 
    assert(!(sa == sc)); 

    char const* d = "ab"; 
    MyString sd(d, 2); 
    assert(!(sa == sd)); 
} 

祝你好運!