Pointer Compilation problem

Hey guys i am facing really big problem with pointer.
I made a programme.It worked first but if i recompile the same programme without
editing and run then it doesn't works.It says "Windows has stopped working";
But then suddenly after another compilation it works again..Sometimes it works and some times not.I got this problem a few days ago too with another programme associating pointer.If anyone know this pz help me.

Here is the code
#include<bits/stdc++.h>
using namespace std;
class strtype{
char *p;
int len;
public:
strtype(char *ptr);
~strtype(){delete []p;};
friend ostream &operator<<(ostream &stream,strtype ob);
};
strtype::strtype(char *ptr){
len=strlen(ptr)+1;
p=new char [len];
if(!p){
cout<<"Allocation error\n";
exit(1);
}
strcpy(p,ptr);
}
ostream &operator<<(ostream &stream,strtype ob){
stream<<ob.p<<endl;
return stream;
}
int main(){
strtype s1("Hello World"), s2("I like c/c++");
cout<<s1<<s2;
}
Last edited on
1
2
ostream &operator<<(ostream &stream,strtype ob)
{
Notice that you are passing an object by value. That means a copy constructor is called. As you did not define a copy constructor, default one is used. And your class will not work with it.
Remember rule of five/three:
If your class defines one of the following: destructor, copy/move constructor, copy/move assigment operator, it should define all of them.
I got it now.Thank you MiiNiPaa
Topic archived. No new replies allowed.