run_time error

This is a discussion on run_time error within the C++ Programming forums, part of the General Programming Boards category; 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 ...

  1. #1
    cert
    Guest

    Question 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:perator=(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;
    }

  2. #2
    the hat of redundancy hat nvoigt's Avatar
    Join Date
    Aug 2001
    Location
    Hannover, Germany
    Posts
    3,138
    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)
    {
        
    // do something to make this equal a
        
    return *this;

    hth
    -nv

    She was so Blonde, she spent 20 minutes looking at the orange juice can because it said "Concentrate."

    When in doubt, read the FAQ.
    Then ask a smart question.

  3. #3
    Unregistered
    Guest

    Question

    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.

  4. #4
    the hat of redundancy hat nvoigt's Avatar
    Join Date
    Aug 2001
    Location
    Hannover, Germany
    Posts
    3,138
    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 ?
    hth
    -nv

    She was so Blonde, she spent 20 minutes looking at the orange juice can because it said "Concentrate."

    When in doubt, read the FAQ.
    Then ask a smart question.

  5. #5
    Registered User
    Join Date
    Mar 2002
    Posts
    1,595
    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.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Getting an error with OpenGL: collect2: ld returned 1 exit status
    By Lorgon Jortle in forum C++ Programming
    Replies: 6
    Last Post: 05-08-2009, 08:18 PM
  2. An error is driving me nuts!
    By ulillillia in forum C Programming
    Replies: 5
    Last Post: 04-04-2009, 09:15 PM
  3. Making C DLL using MSVC++ 2005
    By chico1st in forum C Programming
    Replies: 26
    Last Post: 05-28-2008, 01:17 PM
  4. Connecting to a mysql server and querying problem
    By Diod in forum C++ Programming
    Replies: 8
    Last Post: 02-13-2006, 09:33 AM
  5. Couple C questions :)
    By Divx in forum C Programming
    Replies: 5
    Last Post: 01-28-2003, 12:10 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21