Thread: String class problem

  1. #1
    Registered User
    Join Date
    Aug 2007
    Posts
    4

    String class problem

    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!
    Last edited by Front242; 08-17-2007 at 07:29 AM.

  2. #2
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    You may want to implemet:
    Code:
        mystring& operator=(const unsigned char*);
    Edit: Ehm, of course that won't help. What you're trying to do doesn't work for a class like that - at least I'm not aware of a way to making that work. If you really want to assign a "mystring" to a binary random data value, then you'll probably have to implement that as a member function - but none of the rest of your code will cope very well with random data values, e.g. the address of value may well have a zero in it, which "ends the string" when you do strcmp/strcpy.

    --
    Mats
    Last edited by matsp; 08-17-2007 at 07:25 AM.

  3. #3
    Registered User
    Join Date
    Aug 2007
    Posts
    4
    Thank you Mats, I forgot to tell that I have tried this already but it does not solve the problem.

  4. #4
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,895
    Um, just what do you want to achieve with this bit of code? It doesn't make even the slightest bit of sense.
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  5. #5
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by CornedBee View Post
    Um, just what do you want to achieve with this bit of code? It doesn't make even the slightest bit of sense.
    I agree. First we have an integer, then we take the address of that, cast it to a pointer to unsigned char, and assign it to a class-pointer - all recipe for crashing and nothing useful will ever come out of it.

    To get rid of the compiler error, try replacing unsigned char * with mystring * - but it's going to CRASH when you run it.

    --
    Mats

  6. #6
    Registered User
    Join Date
    Aug 2007
    Posts
    4
    Code:
    /*Found in Handbook for Debugging and Optimizing Legacy.*/
    
    #include<stdio.h>
    void main(int argc, char *argv[])
    {
         unsigned long value = 0xABCDEF12;
         unsigned char *arr;
         arr = (unsigned char *)&value;
         printf("&#37;X %X %X %X\n",arr[0],arr[1],arr[2],arr[3]);
         return;
    }
    If you are on a 32-bit 8x86 Intel-based platform, this program should print out the following:

    12 EF CD AB

    I was just playing around a bit with big-endian versus little-endian.
    If I add a define mystring to unsigned char then it works but I was wondering if I could solve the problem within the string class.
    Last edited by Front242; 08-17-2007 at 07:46 AM.

  7. #7
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,895
    No. The string class is there to handle such stupid, undefined behaviour better, i.e. by not compiling.
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  8. #8
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by Front242 View Post
    Code:
    /*Found in Handbook for Debugging and Optimizing Legacy.*/
    
    #include<stdio.h>
    void main(int argc, char *argv[])
    {
         unsigned long value = 0xABCDEF12;
         unsigned char *arr;
         arr = (unsigned char *)&value;
         printf("%X %X %X %X\n",arr[0],arr[1],arr[2],arr[3]);
         return;
    }
    If you are on a 32-bit 8x86 Intel-based platform, this program should print out the following:

    12 EF CD AB

    I was just playing around a bit with big-endian versus little-endian.
    If I add a define mystring to unsigned char then it works but I was wondering if I could solve the problem within the string class.
    Short answer: No you can't, because it's not a string.

    Long answer:
    That's a valid way to make a char-pointer point at a the address of the unsigned long "value".

    Your string class isn't ever a char-pointer (in the code above, char pointer is NOT used to point to a string - it is pointing at a block of data that is 4 bytes long [it is assumed in the code - on a 64-bit machine, unsigned long is likely to be 8 bytes, and if it's a big endian machine, the output would be 0 0 0 0 as the upper part of the address is then zero].

    Since the data you are pointing at isn't a string, it's pretty meaningless to use a string class for it - whatever you with it later, the string class isn't going to help you get the job done.

    --
    Mats

  9. #9
    Registered User
    Join Date
    Aug 2007
    Posts
    4
    Thank you Mats for the explanation.

    Have a nice day!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. String issues
    By The_professor in forum C++ Programming
    Replies: 7
    Last Post: 06-12-2007, 09:11 AM
  2. Dikumud
    By maxorator in forum C++ Programming
    Replies: 1
    Last Post: 10-01-2005, 06:39 AM
  3. Headers that use each other
    By nickname_changed in forum C++ Programming
    Replies: 7
    Last Post: 10-03-2003, 04:25 AM
  4. Another overloading "<<" problem
    By alphaoide in forum C++ Programming
    Replies: 18
    Last Post: 09-30-2003, 10:32 AM
  5. gcc problem
    By bjdea1 in forum Linux Programming
    Replies: 13
    Last Post: 04-29-2002, 06:51 PM