mercredi 2 décembre 2020

Condition inside if-statement not being satisfied, but is still executing

So mostly everything is starting to work with my vector class but I've run into one more small problem. Inside of my overloaded == operator, I have an if-statement that is executing even though it shouldn't be.

Here it is,

bool myStringVector::operator==(const myStringVector& data){
    std::cout << this->size() << " " << data.size() << std::endl;
    if(this->size() != data.size())
      return 0;
    for(int i = 0; i < data.size(); ++i){
      std::cout << *(base+i) << " " << *(data.base+i) << ";" << std::endl;
      if(*(base+i) != *(data.base+i)){
        std::cout << *(base+i) << " " << *(data.base+i) << ";" << std::endl;
        return 0;
      }
    }
    return 1;
  }

and main...

myStringVector v1 {"a", "b", "c"};
//std::cout << v1[0] << v1[1] << v1[2] << std::endl;
myStringVector v2 = v1;
//std::cout << "here?\n";
//std::cout << v1[0] << v1[1] << v1[2] << std::endl;
//std::cout << v2[0] << v2[1] << v2[2] << std::endl;
assert(v1 == v2);
std::cout << "Passed copy ctor" << std::endl;

I added some cout statements to check whether or not the values are correct or not. I also check the size of both objects. When I run the program, heres what I get...

3 3

a a;

a a;

Assertion failed: v1 == v2, file myCodeTres.cpp, line 60

Im pretty sure my logic is right here. Both *base and *(data.base) contain 'a', so they should be equal. And I wrote this function to only return with 0 if they are not equal. Inside of these vectors are myString objects. I believe I've fixed the issues I had with my string class in response to my last question. But if you believe my custom string class is still a part of the problem let me know.

If you need me to upload any other functions for my vector class let me know! I wanted to try and keep this post as short as possible.

Aucun commentaire:

Enregistrer un commentaire