Thread: Operator Overloading (+) - problem in assignment

  1. #1
    Registered User
    Join Date
    Feb 2012
    Posts
    17

    Operator Overloading (+) - problem in assignment

    Here's my code:

    Code:
    #include <iostream.h>#include <conio.h>
    #include <string.h>
    
    
    class string {
        char *str;
        int len;
    public:
        string(){}
        string(char *a) {
            len = strlen(a);
            str = new char[len+1];
            strcpy(str, a);
        }
        string (string &a) {
            len = a.len;
            str = new char[len+1];
            strcpy(str, a.str);
        }
        ~string()
        { delete str; }
    
    
        string operator+(string);
    
    
        void display()
        { cout << str; }
    };
    
    
    int main() {
        string s1("Hello, "), s2("how are you?");
        clrscr();
        cout << "\n\nName: Sourabh Verma | Roll Number: 2K11-MRCE-CSE-116\n\n";
        string s3 = s1 + s2; // Use Reference Here !!!!!
        return 0;
    }
    
    
    string string :: operator+(string a) {
        string temp;
        temp.len = a.len + len + 1;
        strcpy(temp.str, str);
        strcat(temp.str, a.str);
        return temp;
    }
    I get this runtime error: "Null Pointer Assignmenw"
    Notice the 'w' at the end ... weird, I know !!

    How can I do the assigning at line marked with comment with reference ?

  2. #2
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    You are using an ancient compiler. First, you have to update. Get Visual Studio, gcc or clang.
    This is to make sure you can written standards compliant code.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  3. #3
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,612
    I would create another constructor that can make a string of specific length. Then you might succeed.

    Code:
    class string {
       public:
          friend string operator + (const string &a, const string &b);
          // whatever else
    };
    
    string operator + (const string &a, const string &b) {
       string temp(a.length + b.length + 1);
       strcpy(temp.str, a.str);
       strcat(temp.str, b.str);
       return temp;
    }

  4. #4
    Registered User
    Join Date
    Feb 2012
    Posts
    17
    Quote Originally Posted by Elysia View Post
    You are using an ancient compiler. First, you have to update. Get Visual Studio, gcc or clang.
    This is to make sure you can written standards compliant code.
    I know I am using an outdated complier but my school wants the c++ code in that outdated form only. So i have to work on it. I do learn new standards simultaneously too.

  5. #5
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,817
    Code:
    string string :: operator+(string a) {
        string temp;
        temp.len = a.len + len + 1;
        strcpy(temp.str, str);
        strcat(temp.str, a.str);
        return temp;
    }
    When temp is created, it uses the default constructor which does nothing according to your code - I would at the very least like to see the len data member initialized to 0 and the pointer initialized to null. That aside, please note that temp's str pointer is uninitialized and contains a random value (address). Then you attempt to strcpy/strcat to that uninitialized/random pointer. I hope you understand that this is bad, writing to a random address in memory.
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Overloading assignment operator
    By ryanmcclure4 in forum C++ Programming
    Replies: 20
    Last Post: 10-28-2012, 09:44 AM
  2. assignment operator overloading problem
    By auralius in forum C++ Programming
    Replies: 3
    Last Post: 05-19-2011, 02:33 AM
  3. Overloading Assignment Operator, this pointer
    By cuo741 in forum C++ Programming
    Replies: 11
    Last Post: 12-09-2010, 09:12 AM
  4. Assignment operator overloading
    By hsgw in forum C++ Programming
    Replies: 1
    Last Post: 01-20-2007, 06:44 PM
  5. overloading the assignment operator
    By Unregistered36 in forum C++ Programming
    Replies: 1
    Last Post: 11-30-2001, 06:51 AM