Hi,
I was recently testing a class that I had created and I can't figure out why it won't print the message that I want it to. The output of the program is supposed to be "Hello World" but when I run it, all I have is goop! Here is a copy of my code:
struct Dude
{
char * p;
Dude(); {p = new char[100]; strcpy(p, "Hello World"}
Dude(const Dude & dude)
{ p = new char [strlen(dude.p) + 1]; strcpy(p, x.p);}
Dude &operator=(const Dude & dude)
{ delete []p; p = new char[strlen(x.p)+1];
strcpy(p,x.p); return *this;}
~Dude() {delete []p;}
};
void bug(Dude & dude1, Dude &dude2)
{
dude1 = dude2;
}
int main()
{
Dude Man;
bug(Man, Man);
printf ("%s\n", Man.p);
return 0;
}
I cant seem to figure out what I did wrong. Any help is greatly appreciated!! Thank you!
I was recently testing a class that I had created and I can't figure out why it won't print the message that I want it to. The output of the program is supposed to be "Hello World" but when I run it, all I have is goop! Here is a copy of my code:
struct Dude
{
char * p;
Dude(); {p = new char[100]; strcpy(p, "Hello World"}
Dude(const Dude & dude)
{ p = new char [strlen(dude.p) + 1]; strcpy(p, x.p);}
Dude &operator=(const Dude & dude)
{ delete []p; p = new char[strlen(x.p)+1];
strcpy(p,x.p); return *this;}
~Dude() {delete []p;}
};
void bug(Dude & dude1, Dude &dude2)
{
dude1 = dude2;
}
int main()
{
Dude Man;
bug(Man, Man);
printf ("%s\n", Man.p);
return 0;
}
I cant seem to figure out what I did wrong. Any help is greatly appreciated!! Thank you!