0

I need to do some logical comparison and return a boolean answer.

Here is the code from the .cpp file:

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;
}

Here is what is called from main.cpp file:

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

Here are there compiling errors I get:

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
  • 2
    if(this[i] == other[i]) return true; This is going to cause you problems later. Think about what you're doing there. Commented Apr 28, 2012 at 22:59
  • That is actually the only error I am getting now. All I want to do is compare the contents of both the strings. How could I possibly do that without having to overload the [] operators as well? Commented Apr 29, 2012 at 0:01

3 Answers 3

4

this is a pointer, hence you have to dereference it:

this->Size;

Also I think that logic of your operator== is flawed - here, it returns true if any of characters is equal to character on same position in second string. Change your loop to

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

                        return false;
        }

and put return true; instead of last part of your code (else clause) to compare entire strings.

As Seth mentioned, you can't use operator[] on this as above - this way it's treated as array (i.e. this[i] is really *(this + i) - so not what's you are thinking it is). Access your internal storage member instead.

Sign up to request clarification or add additional context in comments.

2 Comments

You told him not to use this.Size, then you used it in your example? Also, (*this)[i] is a perfectly reasonable option (in case operator[] does something more complicated than indexing an array). I don't think the loop termination condition is right either.
Or even better, use std::equal.
2

Problems with your code:

  • this[i]: You apparently want to access the ith character of the string here. This isn't doing that. Assuming your class overloads operator[], you want (*this)[i]. Alternatively, you could directly access the internal representation of the string.

  • if(this[i] == other[i]) return true;: Think about what this means with respect to comparing the strings "A1" and "AB".

  • for () {...}: What happens when you exit the loop? You need to return something if the comparisons manage to make it through the loop without returning.

5 Comments

the class actually doesn't overload operator [], in this case, what should I do? For this loop I just want to check if the two strings are the same in length and the same in content. If so, return true.
You access the internal representation of your string. You certainly have some data member that stores the contents of the string. So access it.
That's what I'm confused on and need help with.
You didn't show your class MyString. How are we supposed to know which element to access? I suggest you enjoy the weekend and go see a tutor on Monday. You appear to have deeper misunderstandings of objects and of C++ than how to overload operator ==.
(*this)[i] and this->[i] are very much not the same!
0

You haven't specified if you can use the C++ standard algorithms or not. Here you have illustrated both versions, using hand-written loop and std::equal algorithm:

//#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));
}

Good luck!

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.