-3

I made my own class string it has two attributes buff to store string and length when i compile my code without it works fine but if i use it String as an object i get alot of errors what is the reason for the errors and how can i prevent them thank you

    #include <iostream>
using namespace std;
class String
{
private:
    int length;
    char *buff;
public:

    String operator=(String &);
    String();
    String(String &);
    String(char *);
    int size(char *);
    void copy(char *);
    char getvalue(int);
    char *getbuff(){return buff;}
    void setindex(char,int);
    int getlength();
    void display();
    ~String();
};

String :: String()
{
    length = 1;
    buff = 0;
}

String :: String(String &temp)
{

    length = size(temp.buff);
    buff = new char[temp.length + 1];
    copy(temp.buff);
}

String :: String(char *a)
{
    length = size(a);
    buff = new char [length + 1];
    copy(a);
}

int String :: size(char *a)
{
    int i;
    for(i = 0;a[i] != '\0';i++)
    {
    }
    return i;
}
void String :: copy(char *a)
{
    delete []buff;
    int i;
    length = size(a);
    buff = new char[length + 1];
    for (i=0;i<length;i++)
    {
        buff[i] = a[i];
    }
    buff[i] = '\0';
}

char String :: getvalue(int index)
{
    return buff[index];
}

void String :: setindex(char value,int index)
{
    buff[index] = value;
}

int String :: getlength()
{
    return length;

}
void  String :: display()
        {
            for (int i = 0;i<length;i++)
                cout << buff[i];
        }

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

String String :: operator=(String &temp)
{
    copy(temp.buff);
    return *this;
}

void main()
{
    String a("r");
    String b("ee");
    b = a;
    b.display();
}
trincot
  • 317,000
  • 35
  • 244
  • 286
user1948105
  • 1
  • 1
  • 4

2 Answers2

1

Your primary error is that you have not overloaded the copy constructor.

There are other things wrong with your class but that is the one you are probably experiencing.

When you are calling b.copy(a), the function is accepting a String by value. That means it makes a copy.

The default copy constructor copies member-by-member. And both your members will have the same pointer as buff. And both will try to delete this, causing a double delete.

For more information about your error, see Rule Of Three

Community
  • 1
  • 1
CashCow
  • 30,981
  • 5
  • 61
  • 92
  • if i call it by reference will it solve the problem it did but why did it work – user1948105 Jan 04 '13 at 12:24
  • @user1948105: What do you mean "call it by reference"? You mean you change the parameter type of `copy` to a `String&`? No, it will not solve the problem, it will only hide it. Your class will still be broken. Any function that takes one of your String objects by value will reveal the error again. – Benjamin Lindley Jan 04 '13 at 12:33
  • @BenjaminLindley then how can i solve it – user1948105 Jan 04 '13 at 12:35
  • @user1948105: Did you read the CashCow's answer? The answer that we are commenting on? – Benjamin Lindley Jan 04 '13 at 12:36
  • @BenjaminLindley yes i did – user1948105 Jan 04 '13 at 12:38
  • @user1948105: Including the page that he linked to? About the Rule of Three? If so, what part do you not understand? Because it explains how to solve your problem. – Benjamin Lindley Jan 04 '13 at 12:40
  • i did not see the link now i do i will read it now – user1948105 Jan 04 '13 at 12:42
  • @BenjaminLindley My problem is not the copy constructer my problem when using string as compostion i did this exampel because i get the same errir in both – user1948105 Jan 04 '13 at 12:43
  • @user1948105: Well, I guess CashCow and I have no idea what we're talking about. You're the expert, so why are you asking us? – Benjamin Lindley Jan 04 '13 at 12:45
  • i am sorry if that is what you understood all i am saying is aside form the class string i have a class item that has an object of type string my string because the code is too long i made the function copy but the problem that i is facing is every time i use an object of type is string – user1948105 Jan 04 '13 at 12:49
  • @user1948105: Let me be clear. Whatever other problems you happen to be facing, the most important one right now is that your class does not follow the Rule of Three. Fix that, then see if your other problems don't just go away. If they don't, then come back with your new code. – Benjamin Lindley Jan 04 '13 at 12:52
  • @BenjaminLindley i did edit my code to what i hope to be as in the rule of three is what i did correct ? – user1948105 Jan 04 '13 at 13:28
  • @user1948105: It needs a lot of work, like const correctness, but it's definitely an improvement. Do you still have errors? – Benjamin Lindley Jan 05 '13 at 04:14
1

You didn't follow rule of three, there is one copy in below function, which causes the crash.

void copy(String a)

How ever, you meant to copy data only:

b.copy(a.getbuff());

you could also pass reference of a to copy function:

void copy(String& a);
b.copy(a); // it should be safe now
Community
  • 1
  • 1
billz
  • 44,644
  • 9
  • 83
  • 100