Hello
I've got a task to write a simple prototype of string class, but unfortunately I have no idea how to do some things.
This is how it looks now:
Code:
#include <iostream>
class myString {
public:
int length;
char* content;
myString();
myString(const char*);
~myString();
};
myString::myString() {
content = 0;
length = 0;
}
myString::~myString() {
delete[] content;
}
int stringLength(const char* number) {
int c = 0;
while (*(number++)) c++;
return c;
}
myString::myString(const char* number) {
length = stringLength(number);
content = new char[length + 1];
for (int k = 0; k < length; k++)
content[k] = number[k];
content[length] = '\0';
}
std::ostream& operator<<(std::ostream& out, const myString& towrite) {
out << towrite.content;
return out;
}
int main()
{
myString name = "ezample";
std::cout << name;
std::cout << name[0]; //here not working
return 0;
}
My problem is in main() function - I thought that accessing string via indexes is possible, but it's not. And, unfortunately, I have no idea what to do with that. This is char * array, so I thought that there is no problem with printing particular characters. Maybe there is a simple explanation for this?
Also, I wanted to implement std::istream &operator for this class, it works, but I don't know if it's okay, so if anyone could check it I'd be grateful.
Code:
std::istream& operator>>(std::istream& in, const myString& toload) {
in >> toload.content;
return in;
}
Thanks for any advice and help.
The error that appears in main: Error (active) : E0349 no operator "[]" matches these operands. Operands types are myString [ int ]