Hello,
I am using a string class but get an error message when trying to compile the following example:
Code:
unsigned long value = 0xABCDEF12;
mystring *arr;
arr = (unsigned char *)&value;
printf("%X %X %X %X\n",arr[0],arr[1],arr[2],arr[3]);
The error message is: can not convert unsigned char* to mystring*
Here is the string class:
Code:
class mystring
{
protected:
char *str;
public:
mystring();
mystring(const char*);
mystring(const unsigned char*);
mystring(const mystring&);
mystring(const char*, const char*);
~mystring();
int32 length() { return strlen(str); }
char* tomystring() { return str; }
mystring& operator=(const char*);
mystring& operator=(const unsigned char*);
mystring& operator=(const mystring&);
friend mystring operator+(const char*, const mystring&);
friend mystring operator+(const mystring&, const char*);
friend mystring operator+(const mystring&, const mystring&);
friend IOSTREAM& operator<<(IOSTREAM& os, const mystring&);
friend int operator<(const mystring&, const mystring&);
friend int operator>(const mystring&, const mystring&);
friend int operator==(const mystring&, const mystring&);
friend int operator>=(const mystring&, const mystring&);
friend int operator<=(const mystring&, const mystring&);
friend int operator!=(const mystring&, const mystring&);
};
mystring::mystring() {
str = (char*)calloc(1,1);
}
mystring::~mystring() {
free(str);
}
mystring::mystring(const char* s) {
str = strdup(s);
}
mystring::mystring(const mystring& s) {
str = strdup(s.str);
}
mystring::mystring(const char* s1, const char* s2) {
str = (char*)malloc(strlen(s1)+strlen(s2)+1);
strcpy(str, s1);
strcat(str, s2);
}
mystring& mystring::operator=(const char* s) {
str = (char*)realloc(str, strlen(s)+1);
strcpy(str, s);
return *this;
}
mystring& mystring::operator=(const mystring& s) {
str = (char*)realloc(str, strlen(s.str)+1);
strcpy(str, s.str);
return *this;
}
mystring operator+(const char* s, const mystring& rqs) {
mystring *rstr = new mystring(s, rqs.str);
return *rstr;
}
mystring operator+(const mystring& rqs, const char* s) {
mystring *rstr = new mystring(rqs.str, s);
return *rstr;
}
mystring operator+(const mystring& rqs1, const mystring& rqs2) {
mystring *rstr = new mystring(rqs1.str, rqs2.str);
return *rstr;
}
IOSTREAM& operator<<(IOSTREAM& os, const mystring& s) {
return os << s.str;
}
int operator>(const mystring& s1, const mystring& s2) {
if (strcmp(s1.str, s2.str) > 0) return -1;
return 0;
}
int operator<(const mystring& s1, const mystring& s2) {
if (strcmp(s1.str, s2.str) < 0) return -1;
return 0;
}
int operator==(const mystring& s1, const mystring& s2) {
if (strcmp(s1.str, s2.str) == 0) return -1;
return 0;
}
int operator>=(const mystring& s1, const mystring& s2) {
if (strcmp(s1.str, s2.str) >= 0) return -1;
return 0;
}
int operator<=(const mystring& s1, const mystring& s2) {
if (strcmp(s1.str, s2.str) <= 0) return -1;
return 0;
}
int operator!=(const mystring& s1, const mystring& s2) {
if (strcmp(s1.str, s2.str) != 0) return -1;
return 0;
}
I hope someone can tell me what is wrong with this class.
Thanks in advance!