I have a String struct that I overloaded the subscript operator on. But it doesn't seem to work.
//my_string.h
struct String {
char* Text;
uint64 Length;
char& operator[](int32 index);
}
//my_string.cpp
char& String::operator[](int32 index) {
ASSERT(index >= 0);
return Text[index];
}
//main.cpp
String* test = string_create("Hello world");
char c = test[0];
Visual Studio gives me the following error:
no suitable conversion function from "String" to "char" exists
The compiler issued an error because in this statement
the expression
test[0]has the typeString.In this declaration
you declared a pointer instead of an object of the type String.
If it is not a typo then in this case you have to write
or
Also it looks strange that the data member
Lengthhas the typeuint64while the index used in the operator has the type
int32.