Thread: Need help with simple overloaded functions and pointers

  1. #1
    Registered User
    Join Date
    Oct 2001

    Need help with simple overloaded functions and pointers

    I am stuck on this part that i am posting. It's just eating me alive. I am trying to make the private pointer data == a given string, but nothing i try works. I have provided the functions needed to test the problem function, and the header file.

    #include "Mstring.h"
    #include <iostream.h>
    #include <cstring>
    #include <cstddef>

    //----->I am stuck on this function<------------
    const MString& MString:perator= (const char *p)
    //needs to be fixed
    //Postcondition: data[] == p[] and length==strlen(p),returns MString

    MString temp;
    delete []; char[temp.length+1];
    for (int i=0; i<temp.length+1; i++)[i]=p[i];
    return temp;

    //---------------> stuck on above function----------------

    MString:: MString()
    //Postcondition: length==0 and data ==NULL
    data=new char;

    MString::MString(const char *p)
    //Postcondition: length == length of *p and *p is copied to data
    data=new char[length+1];
    //for(int i=0; i<length+1; i++)
    // data[i]=p[i];
    MString::MString(const MString& copy)
    //copy constructor
    //Postcondition: length==copy.length and data points to a duplicate of copy's data
    data=new char[length+1];
    //for(int i=0; i<length+1; i++)
    // data[i][i];
    //Postcondition: deletes string
    delete [] data;
    int MString::Length() const
    //Postcondition: returns length of string
    return length;
    ostream & operator << (ostream & out, const MString &o)
    //overloaded cout operator
    //Postcondition: preforms cout for class MString

    for(int i=0; i<o.length+1; i++)
    out <<[i];
    return out;
    int main()
    MString s1;
    MString s2("Hello world");
    //*********This part does not work***********
    //overloaded assignment
    s1 = "C style string";
    cout << s1 << endl;

    // output: C style string
    cout << s1.Length() << endl;
    // output: 14
    cout << s2 << endl;
    // output: Hello world

    return 0;

    #include <iostream.h>

    class MString
    //input, output
    friend ostream & operator << (ostream & out, const MString &);
    char * data;
    int length;
    MString(const char *p);
    MString(const MString& copy);


    //length method
    int Length() const;

    //overloaded assignment
    const MString& operator= (const char *);


  2. #2
    ¡Amo fútbol!
    Join Date
    Dec 2001
    delete [];

    How are you deleting the array when you declared it to be a char??

  3. #3
    Registered User
    Join Date
    Oct 2001
    first of all, I know that temp does not work and the delete means nothing right now. i want data to point to an array of integers and MString s1 has already been defined.

    I need another way to return *data and length for MString s1. it doesn't accept temporary values.

    This is the main call code i am worried about:
    s1 = "C style string"; //<---here is where i run into trouble
    cout << s1 << endl;

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. pointers
    By InvariantLoop in forum C Programming
    Replies: 13
    Last Post: 02-04-2005, 09:32 AM
  2. Binary Search Trees Part III
    By Prelude in forum A Brief History of
    Replies: 16
    Last Post: 10-02-2004, 03:00 PM
  3. Passing pointers between functions
    By heygirls_uk in forum C Programming
    Replies: 5
    Last Post: 01-09-2004, 06:58 PM
  4. Request for comments
    By Prelude in forum A Brief History of
    Replies: 15
    Last Post: 01-02-2004, 10:33 AM
  5. API "Clean Up" Functions & delete Pointers :: Winsock
    By kuphryn in forum Windows Programming
    Replies: 2
    Last Post: 05-10-2002, 06:53 PM