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]`
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 overloadsoperator[], 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.