-
run_time error
hello firstly.when i compiler this program ,i get run-time error..i couldnt corect it.can u help me?thnx.
i think i must do, *p=*(b.p); for p=b.p; but still have run-time eror.
pls say me.
here is the program....
#include<iostream.h>
class A{
int ai;
public:
A();
A(A&);
~A();
void operator=(A);
int fA() const {return ai;}
};
class B{
A a;
A *p;
public:
B(int);
B(B&);
~B();
A &f5(B);
B& operator=(B& b);
};
A::A(){
cout<<"Message 1" <<endl;
ai=1;
}
A::A(A& a){
cout<<"Message 2" <<endl;
ai=a.ai;
}
A::~A(){
cout<<"Message 3" <<endl;
}
void A::operator=(A a){
cout<<"Message 4" <<endl;
ai=a.ai;
}
B::B(int i){
cout<<"Message 5" <<endl;
p=new A[i];
}
B::B(B& b){
cout<<"Message 6" <<endl;
p=b.p;
a=b.a;
}
B::~B(){
cout<<"Message 7" <<endl;
delete [] p;
}
A &B::f5(B b){
cout<<"Message 8" <<endl;
if(a.fA()>b.a.fA()) a=b.a;
return a;
}
void main(){
A a;
B b1(2),b2(1);
B b3=b1;
b3.f5(b2)=a;
b2=b1;
}
-
When posting code, use "code" or "php" tags around your code. You can get them from the buttons saying "#" and "PHP" above the posting window.
Do you know how to use a debugger ? If not, now is the time to learn it. Do you have a debugger ?
Edit:
The Assignment operator should look like this:
PHP Code:
A A::operator = ( const A& a )
{
// do something to make this equal a
return *this;
}
-
I know how to debug but when all lines in main are executed
it gives error.so its difficult to find that error.
i know assignemnet operator as well but still has problem on it.
i know problem with p=b.p;
but cpouldnt corect.
pls anyone who can help me?
pls say me sth how to do it correct.
-
I don't know what this is for, but some general remarks:
Assignment operators look like described above.
Never return a reference to a private member variable in a class function.
Make your code more readable by giving names that are longer than one letter and more meaningful.
main returns an int. Always.
Do you know which line in your main crashes ? Set messages there, too. Which of your messages show up ?
Do you have a debugger ?
-
The compiler won't create a default constructor for class B because you have declared other constructors. The compiler needs the default constructor in the copy constructor. You need to provide it.
-----------------------------------
This:
B::B(int i){
cout<<"Message 5" <<endl;
p=new A[i];
indicates p will be an array of i members of type A with memory for the elements declared on the free store. I suspect you want p to be pointer to a single type A with ai being assigned value i. If so I would try this:
B::B(int i){
cout<<"Message 5" <<endl;
p=new A(i);
wihich says use the A constructor with the single int argument rather than the default constructor for class A when declaring space.
----------------------------------------
Copy constructors can be tricky so I hope Iget it right. When classes have pointers embedded in them as members you have to be careful when copying or assigning one object to another. If you copy the original pointers contents, which is the address of the object being pointed to, this is called a memberwise or shallow copy. Unfortunately, if the original object being pointed to is destroyed before the copy then the pointer in the copy becomes a stray pointer. You should create a deep copy of the pointer, which copies the value at theaddress being pointed to, rather than the address in the pointer, when copying pointer members. In your case maybe something like this:
B::B(B& b){
cout<<"Message 6" <<endl;
p = new A;
*p = *(b.p);
a=b.a;
}
HTH.