bool operator!= (CustomString &str1, CustomString &str2)
{
    std::vector<string>:: iterator s2 = (str2.vec).begin();



    for(std::vector<string>:: iterator s = (str1.vec).begin(); s != (str1.vec).end(); ++s)
    {



       if( *s2 == *s)
       {
           cout << *s2 << *s << endl;

           return false;
       }


        ++s2;
    }

    return true;
}

I don't know why, but this returns true 100% of the time.

Edited 2 Years Ago by OpenTheTrollGate

line 7 is incorrect -- instead of ++i it should probably be ++s.

does line 14 show that *s and *s2 are the same strings?

Actually, it was ++s, but i mistyped it. I think the problem is that I have a class member called vec containing characters, because I made a new main without using a class variable vector<string> and it seemed to work fine. Also, cout doesn't work when I overload the operator, so I have no idea what's going on. Do we have to use str1->vec instead or something?

Edited 2 Years Ago by OpenTheTrollGate

How you write it is a little unconventional, but should still compile ok. You just have unnecessary parentheses.

for(std::vector<string>:: iterator s = str1.vec.begin(); s != str1.vec.end(); ++s)

I can't really say what the problem is without seeing the whole program.

int main()
{
    CustomString *str = new CustomString("helloworld");

    CustomString *str2 = new CustomString("helloworld");


    if (str != str2)
    {
        cout << "something is wrong" << endl;
    }
    else
    {
        cout << "everything is ok" << endl;
    }

    return 0;
}

class LongInteger
{

    friend bool operator== (CustomString &str1, CustomString &str2);
    friend bool operator!= (CustomString &str1, CustomString &str2);


public:


private:
        vector<string> vec;

};

if (str != str2)

Since the memory for str and str2 are allocated at runtime they two address will never be the same. Lines 10 and 14 are reversed, line 8 will always be true so there is nothing wrong.

Since str1 and str2 are really pointers, here is how to write the code

class CustomString
{
public:

    std::vector<string> vec;
};



class LongInteger
{

    friend bool operator== (CustomString *&str1, CustomString *&str2);
    friend bool operator!= (CustomString *&str1, CustomString *&str2);


public:


private:
    vector<string> vec;

};

bool operator!= (CustomString *&str1, CustomString *&str2)
{
    std::vector<string>::iterator s2 = str2->vec.begin();



    for (std::vector<string>::iterator s = str1->vec.begin(); s != str1->vec.end(); ++s)
    {


*
        if (*s2 == *s)
        {
            cout << *s2 << *s << endl;

            return false;
        }


        ++s2;
    }

    return true;
}

int main()
{
    CustomString *str = new CustomString("helloworld");

    CustomString *str2 = new CustomString("helloworld");


    if (str != str2)
    {
        cout << "everything is ok" << endl;
    }
    else
    {
        cout << "something is wrong" << endl;
    }

    return 0;
}

That still won't compile because the two operator lines in class LongInteger are incorrect.

So I need to use *&, what's the name of such construct, pointer address, and how is it different from a pointer(which should store an address)? It's kinda confusing. How about ** instead of *&? Also isn't (str.vec) the same as str->vec?

Edited 2 Years Ago by OpenTheTrollGate

error: 'bool operator== (CustomString *&, CustomString *&)' must have an argument of class or enumerated type
     friend bool operator== (CustomString *&str1, CustomString *&str2)


                                                         ^

Like I said, that won't compile for the reason you just posted. In your program one of the parameters to the friend function must be a reference to LargeInteger class. If that is not needed, then don't make it a friend function.

So I need to use *&, what's the name of such construct, pointer address, and how is it different from a pointer(

The & makes it a reference to a pointer, something similar to **. Since str1 and str2 are both pointers, you can do away with the & operator and just pass the pointers.

I forgot to ask about something though. If I don't make it a friend function, I can't pass two arguments and if it's not a class function nor a friend function, I can't overload. So what are you suggesting me to do?

Make the two friend functions friends of CustomString, not LongInteger. In main(), don't use pointers.

class CustomString
{
public:
    CustomString(std::string s) { SetStr(s); }
    CustomString();
    void SetStr(std::string s) { vec.push_back(s); }
    bool friend operator!= (CustomString& str1, CustomString& str2);
    bool friend operator== (CustomString& str1, CustomString& str2);

    std::vector<string> vec;
};




bool operator== (CustomString& str1, CustomString& str2)
{
    if (str1.vec.size() != str2.vec.size())
    {
        return false;
    }
    std::vector<string>::iterator s2 = str2.vec.begin();

    bool result = true;

    for (std::vector<string>::iterator s = str1.vec.begin(); s != str1.vec.end(); ++s)
    {



        if (*s2 != *s)
        {
            cout << *s2 << ' ' << *s << endl;

            result = false;
        }


        ++s2;
    }

    return result;
}


bool operator!= (CustomString& str1, CustomString& str2)
{
    return str1 == str2 ? false : true;
}

int main()
{
    CustomString str("helloworld");


    CustomString str2("helloworld");

    str.SetStr("One");
    str2.SetStr("Two");

    if(str != str2)
    {
        cout << "everything is ok" << endl;
    }
    else
    {
        cout << "something is wrong" << endl;
    }

    return 0;
}

Edited 2 Years Ago by Ancient Dragon

This question has already been answered. Start a new discussion instead.