Need help with homemade String

This is a discussion on Need help with homemade String within the C++ Programming forums, part of the General Programming Boards category; I need help with this homemade String. I can't get it work. I am told not to do anything with ...

  1. #1
    Registered User
    Join Date
    Feb 2002
    Posts
    37

    Need help with homemade String

    I need help with this homemade String. I can't get it work. I am told not to do anything with the main.cpp, only String.cpp and String.h am I allowed to do what ever I want. This is what I have done so far.

    ////////////////////////////////////////////////////////////////
    // String.h
    #ifndef _STRING_H_
    #define _STRING_H_

    #include <iostream>

    using std:stream;

    class String
    {
    private:
    char * str;
    int len;
    public:
    String();
    String(String const &s);
    String(char const* s);
    ~String();
    String &operator=(String const &s);
    friend ostream &operator<<(ostream &o, String const &s);
    String operator+(String const &s) const;
    };

    #endif

    /////////////////////////////////////////////////////////
    // String.cpp
    #include <iostream>
    #include "String.h"

    String::String()
    {
    str = new char[1];
    str[0] = '\0';
    len = 1;
    }

    String::String(String const &s)
    {
    str = new char[0];
    *this = s;
    }

    String::String(const char* s)
    {
    len = strlen(s);
    str = new char[len + 1];
    strcpy(str, s);
    }

    String & String:perator=(String const &s)
    {
    if (this != &s)
    {
    delete [] str;
    str = new char[s.len + 1];
    for (int i = 0; i <= s.len; i += 1)
    str[i] = s.str[i];
    len = s.len;
    }
    return *this;
    }

    String String:perator +(String const &s) const
    {
    char *temp = new char[len + s.len + 1];
    for (int i = 0; i < len; i += 1)
    temp[i] = str[i];
    for (i = 0; i <= s.len; i += 1)
    temp[i + len] = s.str[i];
    String res(temp);
    return res;
    }

    String::~String()
    {
    delete [] str;
    }

    ostream &operator<<(ostream &o, String const &s)
    {
    o << s.str;
    return o;
    }

    ///////////////////////////////////////////////////////////////
    // main.cpp
    #include "String.h"
    #include <iostream>

    using std::cout;
    using std::endl;

    int main()
    {
    String t1("Donald");
    String t2;
    String t3(t1);
    t2 = "Here comes " + t1; // I get an error message
    t3 = t2 + " Duck";
    cout << t1 << endl;
    cout << t2 << endl;
    cout << t3 << endl;

    return 0;
    }

    // Error message in MS Visual C++ 6.0 sp5, Windows 2000
    // binary '+': no global operator defined which takes
    // type 'class String' for there is no acceptable conversion


    Thanks.
    Mikael Raja
    zzzazzz@hotmail.com

  2. #2
    Clearn
    Guest
    you got to write a global function like this..

    String& operator+(char * charstr,String& strOB){return strOB;}

    should be a 'friend' if it accesses any of private members.

  3. #3
    Registered User
    Join Date
    Feb 2002
    Posts
    37
    Ok that fixed the error but end result is.
    //////////////////////////////////////////////
    Donald
    Donald
    Donald Duck
    /////////////////////////////////////////////

    I want the end result to be
    /////////////////////////////////////////////
    Donald
    Here comes Donald
    Here comes Donald Duck
    /////////////////////////////////////////////

    How do I do that, because I don't seem to understand where things go wrong this time.

    Thanks.
    Mikael Raja
    zzzazzz@hotmail.com

  4. #4
    Clearn
    Guest

    Exclamation

    I gave you that as an example..

    String& operator+(char * charstr,String& strOB)
    {
    // Please write some code here to add
    // 'charstr' and 'strOB.str' and copy it
    // to 'strOB.str' ....!!

    return strOB;}

    I think this will help you..

  5. #5
    Registered User
    Join Date
    Feb 2002
    Posts
    37
    Ok now my project look like this

    ////////////////////////////////////////
    // String.h

    #ifndef _STRING_H_
    #define _STRING_H_

    #include <iostream>

    using std:stream;

    class String
    {
    private:
    char * str;
    int len;
    public:
    String();
    String(String const &s);
    String(char const* s);
    ~String();
    String &operator=(String const &s);
    friend ostream &operator<<(ostream &o, String const &s);
    String operator+(String const &s) const;
    friend String operator+(char * charstr, String& strOB);
    };

    #endif

    ////////////////////////////////////////////
    // String.cpp

    #include <iostream>
    #include "String.h"
    #include <cstring>

    String::String()
    {
    str = new char[1];
    str[0] = '\0';
    len = 1;
    }

    String::String(String const &s)
    {
    str = new char[0];
    *this = s;
    }

    String::String(const char* s)
    {
    len = strlen(s);
    str = new char[len + 1];
    strcpy(str, s);
    }

    String & String:perator=(String const &s)
    {
    if (this != &s)
    {
    delete [] str;
    str = new char[s.len + 1];
    for (int i = 0; i <= s.len; i += 1)
    str[i] = s.str[i];
    len = s.len;
    }
    return *this;
    }

    String String:perator +(String const &s) const
    {
    char *temp = new char[len + s.len + 1];
    for (int i = 0; i < len; i += 1)
    temp[i] = str[i];
    for (i = 0; i <= s.len; i += 1)
    temp[i + len] = s.str[i];
    String res(temp);
    return res;
    }

    String::~String()
    {
    delete [] str;
    }

    ostream &operator<<(ostream &o, String const &s)
    {
    o << s.str;
    return o;
    }

    String operator+(char * charstr, String& strOB)
    {
    int len = 1000;
    char *temp = new char[len + strOB.len + 1];
    for (int i = 0; i < len; i += 1)
    temp[i] = charstr[i];
    String test(temp);

    return test;
    }

    //////////////////////////////////////////
    // main.cpp

    #include "String.h"
    #include <iostream>

    using std::cout;
    using std::endl;

    int main()
    {
    String t1("Donald");
    String t2;
    String t3(t1);
    t2 = "Here comes " + t1;
    t3 = t2 + " Duck";

    cout << t1 << endl;
    cout << t2 << endl;
    cout << t3 << endl;

    return 0;
    }

    ////////////////////////////////////////////
    // End result

    Donald
    Here comes
    Here comes Duck

    ////////////////////////////////////////////
    // The end result I want

    Donald
    Here comes Donald
    Here comes Donald Duck

    ///////////////////////////////////////////////////

    I must have missed something, but I can't see it.

    Thanks
    Mikael Raja
    zzzazzz@hotmail.com

  6. #6
    Unregistered
    Guest
    I think your current problem is with this line:

    t2 = "Here comes " + t1;

    The way I think this will be interpretted by the compiler is to add t1 to "Here comes " and then assign the result to t2. The problem is "Here comes " is a constant string literal and cannot be modified. Therefore I would try this:

    t2 = "Here comes ";
    t2 = t2 + t1;

  7. #7
    Clearn
    Guest
    #include<string.h>

    String operator+(char *str,String strOB)
    {
    char * cptr=new char[strlen(str)+strlen(strOB.str)+1];
    strcpy(cptr,str);
    strcat(cptr,strOB.str);
    return String(cptr);
    }

  8. #8
    Registered User
    Join Date
    Feb 2002
    Posts
    37
    Thanks. Problem solved.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. C++ ini file reader problems
    By guitarist809 in forum C++ Programming
    Replies: 7
    Last Post: 09-04-2008, 07:02 AM
  2. We Got _DEBUG Errors
    By Tonto in forum Windows Programming
    Replies: 5
    Last Post: 12-22-2006, 05:45 PM
  3. Classes inheretance problem...
    By NANO in forum C++ Programming
    Replies: 12
    Last Post: 12-09-2002, 03:23 PM
  4. creating class, and linking files
    By JCK in forum C++ Programming
    Replies: 12
    Last Post: 12-08-2002, 02:45 PM
  5. Warnings, warnings, warnings?
    By spentdome in forum C Programming
    Replies: 25
    Last Post: 05-27-2002, 07:49 PM

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