-2

I'm creating my own class String in C++. I stacked at this point:

class String{
    char* arr;
    int len;

    String& operator[](int);
}

String& String::operator[](int n) {
len = 1;
arr = new char[1];
arr = *arr[n];
}

The compiler shows me this:

In member function ‘String& String::operator’: error: invalid type argument of ‘unary *’</p>

Where is the bug? Thanks for help! :)

4

1 に答える 1

1

このように sth する必要があると思いますが、それでもコンパイルできない場合はもう一度尋ねてください。

class String{
    char* arr;
    int len;

    char operator[](int);
};

char String::operator[](int n) {
    //if (n >= len) throw;
    //if (arr = NULL) throw;
    return arr[n];
}
于 2013-06-17T22:13:25.430 に答える