2012-04-28 162 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)); 
} 

祝你好运!