Problems with this code!?

i'm getting frustrated with this lines of code, it's something about operator= , everything compiles fine, but program crashes during that crap,i'm still a noob so don't burn me for something obvious,here's the code:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
#include <iostream>
using namespace std;

class String
{
public:
       String(){cout << "Simple string constructor\n";}
       ~String(){};
       String operator+=(String& rhs);
       String operator=(char* rhs);
       char getString();
       char* mstring;
       friend ostream operator<<(ostream& output,String s2);
};

String String::operator+=(String& rhs)
{
    mstring += rhs.getString();   
}

ostream operator<<(ostream& output,String s2)
{    
    output << s2.getString();   
}

String String::operator=(char* rhs)
{
    *mstring = *rhs;   
}

char String::getString()
{
    return *mstring;
}
int main(){
    cout << "Creating s1\n";
    String s1;
    cout << "Adding \"Hi!\" to s1\n";
    s1 = "Hi!";
    cout << "Creating s2\n";
    String s2;
    cout << "Adding \"World\" to s2\n";
    s2 = "World";
    cout << "Test of += operator in string!\n";
    s1 += s2;
    cout << s1;
    cout << "\nDid this work?";
    cin.get();
    return 0;
}
You'd need you allocate a dynamic array at a certain point in your code to do what you are trying to do.
If you are a noob you shouldn't even be attempting this at all. You should be using std::string. It always astounds me when I see a noob trying to implement a custom string or dynamic array class. This is a task that even the most experienced programmers would need to put a great deal of effort into.

You don't have an assignment operator at all. The correct declaration is
String& operator=(const String& rhs);

If character arrays could be copied as easily as how you attempted to do it we wouldn't need a std::string class in the first place. All that you are doing is copying 1 character from the rhs char array to the lhs char array. If you want to copy a c-array you have to use strcpy or strncpy. You also have to ensure that enough memory is allocated on the lhs.

You need to read up on the assignment operator. There are many, many threads available on the subject. That is if you really insist on finishing this. I recommend that you use std::string until you are more experienced.
ok thankx you guys,then i'll just use std::string for a while ;)
@ Kempofighter: hey I aswell am a newbe at advanced coding but im working on it my question is what is the difference between using namespace std; and std::string; personally i like the u.n.s; then the s::s; only because 99% of all my books use the u.n.s; method im not being mean but im just wanting some extra info on the pros and cons between the to thanks

Skillet
You could get ambiguities if you use different libraries or if you define a symbol with the same name as something in the std namespace
Topic archived. No new replies allowed.