Results 1 to 6 of 6

Thread: Mystring Class help..

  1. #1

    Thread Starter
    Member
    Join Date
    Aug 2000
    Posts
    54

    Mystring Class help..

    Hey,
    I have an assignment for my Data structures class to write my own string class using character arrays with Dynamic Memory Allocation. Its way over due, and I still cant figure out how to do much of anything. Does anyone have a simplified string class where I could understand how to create the class and work on changing it to dynamic memory allocation on my own?

  2. #2
    Fanatic Member twanvl's Avatar
    Join Date
    Dec 2001
    Posts
    771
    A string class should look something like this:
    Code:
    class myString
    {
    private:
        char* data;
        unsigned int allocated;
    public:
        //constructor
        myString() : data(0), allocated(0) {}
        myString(const myString& that)
        { /* Allocate memory, copy data from 'that' */ }
        //destructor
        ~mystring()
        { /* Free the memory allocated for 'data' */ }
    
        //operators
        myString& operator = (const myString& that)
        {
            // Ensure that 'data' is large enough, if not, free the current memory, and allocate more memory
            // Copy
        }
        // More functions:
        //    operator += for appending strings
        //    c_str() that returns 'data' with a terminating 0
        //    all of the operators again that take a char* instead of a myString
    };

  3. #3

    Thread Starter
    Member
    Join Date
    Aug 2000
    Posts
    54

    thanks

    Thanks alot.. I should be able to figure it out a little easier now.. I hope..

    -Jason

  4. #4

    Thread Starter
    Member
    Join Date
    Aug 2000
    Posts
    54
    Okay so i'm working with the mystring class. Currently i am trying to overload the extraction operator. I came up with the code below, which I know wont show me the entire string but should display the first character of the string (i'll figure out how to actually do that later). But when I go to compile it with this I get these two error messages.

    "Error E2080 mystring.cpp 28: 'mystring:perator <<(ostream &,const mystring &)' must be declared with one parameter

    Error E2280 mystring.cpp 52: Member identifier expected in function operator <<(ostream &,const mystring &)"


    //Class member description
    ostream& operator <<(ostream&, const mystring&);

    //My implementation of the extraction operator
    ostream& mystring:perator <<(ostream& out, const mystring& tmp)
    {
    out<< tmp.(*theBuffer);
    return out;
    }


    If anyone could explain to me what i'm doing wrong I would really appreciate it. I dont usually have this much trouble with classes, but it all doesnt come easy to me yet.

  5. #5
    Fanatic Member twanvl's Avatar
    Join Date
    Dec 2001
    Posts
    771
    operator << shouldn't be a member of the string class, if it is a member it has 3 parameters (the two you see, and the this pointer)

  6. #6
    Kitten CornedBee's Avatar
    Join Date
    Aug 2001
    Location
    In a microchip!
    Posts
    11,594
    It can't be. If it was it would have only one parameter, but it wouldn't work the way it should.

    It has to be a global function.
    All the buzzt
    CornedBee

    "Writing specifications is like writing a novel. Writing code is like writing poetry."
    - Anonymous, published by Raymond Chen

    Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width