Implementing String class c++

时间:2016-07-11 19:12:52

标签: c++ string class

I am trying to implement a String class in c++. I am trying to overload the "==" operator, I keep getting these errors:

error C2676: binary '[': 'const MyString' does not define this operator or a conversion to a type acceptable to the predefined operator"

and

error C2088: '[': illegal for class

bool operator==(const MyString& str1, const MyString& str2) {

int i;
for (i=0; str1[i] != '\0'; i++){
    if (str1[i] != str2[i]) {
        return false;
    }
}
return true;
}

1 个答案:

答案 0 :(得分:1)

As the error suggests, you have not written an implementation for the [] operator. Try something like this, inside your MyString class:

char& operator[](int index){
    //return character value at given index, perform bounds checking if you like
}