Need help with simple overloaded functions and pointers

This is a discussion on Need help with simple overloaded functions and pointers within the C++ Programming forums, part of the General Programming Boards category; I am stuck on this part that i am posting. It's just eating me alive. I am trying to make ...

  1. #1
    Registered User
    Join Date
    Oct 2001
    Posts
    11

    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.

    //************************MString.cpp***********
    #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;
    temp.length=strlen(p);
    delete [] temp.data;
    temp.data=new char[temp.length+1];
    for (int i=0; i<temp.length+1; i++)
    temp.data[i]=p[i];
    return temp;


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

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

    }
    MString::MString(const char *p)
    //constructor
    //Postcondition: length == length of *p and *p is copied to data
    {
    length=strlen(p);
    data=new char[length+1];
    strcpy(data,p);
    //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
    {
    length=copy.length;
    data=new char[length+1];
    strcpy(data,copy.data);
    //for(int i=0; i<length+1; i++)
    // data[i]=copy.data[i];
    }
    MString::~MString()
    //destructor
    //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 << o.data[i];
    return out;
    }
    //*****************client.cpp*************
    int main()
    {
    //construction
    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;
    }
    //**************MString.h***************

    #include <iostream.h>

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

    //destructor
    ~MString();

    //length method
    int Length() const;

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

    };

  2. #2
    ¡Amo fútbol!
    Join Date
    Dec 2001
    Posts
    2,136
    delete [] temp.data;


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

  3. #3
    Registered User
    Join Date
    Oct 2001
    Posts
    11
    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 Cprogramming.com
    Replies: 16
    Last Post: 10-02-2004, 04: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 Cprogramming.com
    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, 07:53 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21