Thread: I think it's logic error

  1. #1
    Registered User
    Join Date
    Apr 2009

    I think it's logic error

    using namespace std;
    class Account {
    	private :
    	double Balance ;
    	string BlanceType ;
    	public :
    	Account (double d) {
    		Balance = d;
    		BlanceType = "Bronze" ; }
    	void Set ( double B ) {
    		Balance = B ;
    		if ( Balance>= 20000 )
    		{ BlanceType = "Golden" ;}
    		if ( Balance>= 5000 )
    		{ BlanceType = "Silver" ; }
    		{ BlanceType = "Bronze" ; } }
    	int GetBalance ( ) {
    		return Balance ; }
    	string GetBalanceType ( ) {
    		return BlanceType ; }
    void Show(){ cout <<  Balance<< BlanceType; }
    class Customer{
    int numberOfac........s;
    Account *Ac........s;
    Customer(int number)
    numberOfac........s = number;
    Ac........s = new Account[numberOfac........s];
    for (int i=0;i<numberOfac........s; i++)
    { double p;
    cout<<"Enter balence ";
    ~Customer(){delete []Ac........s;}
    Customer operator ++(int)
    {Customer c=*this;
    return c;
    int main()
    	int number;
    cout<<"Enter number of ac........s";
    Customer custom(number);
    	return 0;
    Enter number of ac........s3
    Enter balence 120000
    Enter balence 2000
    Enter balence 2000
    aasd(1335) malloc: *** error for object 0x100200: double free
    *** set a breakpoint in malloc_error_break to debug
    aasd(1335) malloc: *** error for object 0x100150: double free
    *** set a breakpoint in malloc_error_break to debug

  2. #2
    Registered User
    Join Date
    Jan 2005
    You are dynamically allocating memory for your array in your class and destroying it in the destructor. The fact that you need the destructor is a good indication that you also need a copy constructor and a copy assignment operator (see the Rule of Three).

    The problem is that when you create a copy of your object inside operator++, the copy does a shallow copy which only copies the pointer. Since you now have two objects pointing to the same location, when one of those objects goes out of scope the memory is deleted. If the other copy tries to use it then you get a crash (or some other error).

    The best solution is to use vector instead of dynamically allocating your array. The only good reason I've heard for manually allocating your array is if your instructor won't allow you to use vector (which is too bad but not something you can usually control).

    If you don't use vector for whatever reason, then you need to create a copy constructor and copy assignment operator that properly copy the object.

  3. #3
    Registered User
    Join Date
    Apr 2009
    ok i'll try use copy constructor
    but there is something missing in this code and i'm trying to solve it
    i have to increment a custmer account if he has at least one golden account and i have to write it
    in main can you help me please?

  4. #4
    Registered User
    Join Date
    Dec 2006
    You may want to fix your indentation first. Your code is unreadable.

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. Testing some code, lots of errors...
    By Sparrowhawk in forum C Programming
    Replies: 48
    Last Post: 12-15-2008, 04:09 AM
  3. Making C DLL using MSVC++ 2005
    By chico1st in forum C Programming
    Replies: 26
    Last Post: 05-28-2008, 01:17 PM
  4. Linking error
    By DockyD in forum C++ Programming
    Replies: 10
    Last Post: 01-20-2003, 05:27 AM
    By Registered in forum C++ Programming
    Replies: 1
    Last Post: 07-15-2002, 03:23 PM