class error

This is a discussion on class error within the C++ Programming forums, part of the General Programming Boards category; I'm new to classes and i am having some errors heres my codee accountp.cpp The main program Code: #include <cstdlib> ...

  1. #1
    Registered User
    Join Date
    Nov 2007
    Posts
    164

    class error

    I'm new to classes and i am having some errors

    heres my codee

    accountp.cpp

    The main program

    Code:
    #include <cstdlib>
    #include <iostream>
    #include <string>
    
    #include "Account.h"
    
    using namespace std;
    
    int main()
    {
    	Account account1;
        Account account2;
    	cout <<"\n Current Balance for account1 is " << account1.getBalance();
    	cout <<"\n Current Balance for account2 is " << account2.getBalance();
    
    	int amount1, amount2;
    
    	cin >> amount1 >> amount2;
    
    	account1.credit(amount1);
    	account2.credit(amount2);
    
    	cout <<"\n Current Balance for account1 is " << account1.getBalance();
    	cout <<"\n Current Balance for account2 is " << account2.getBalance();
    
    	int amountw1, amountw2;
    
    	account1.debit(amountw1);
    	account1.debit(amountw2);
    
    	cout <<"\n Current Balance for account1 is " << account1.getBalance();
    	cout <<"\n Current Balance for account2 is " << account2.getBalance();
    
    	return 0;
    }
    account.h

    Class definition and function prototypes
    Code:
    #include <iostream>
    #include <cstdlib>
    #include <string>
    
    using namespace std;
    
    // class Account definition
    class Account
    {
    public:
    	Account( int balance ); // constructor
    	void credit( int add_amount ); // function that add an amount to the current balance
    	void debit( int withdraw_amount ); // function that withdraw money from the Account
        int getBalance(); // function that returns the the current balance
    private:
    	int account_balance; // stores the current balance
    };
    account.cpp

    Class member function definition

    Code:
    #include <cstdlib>
    #include <iostream>
    #include <string>
    
    using namespace std;
    
    #include "Account.h" // include definition of class Account
    
    // constructor
    Account::Account( int balance )
    {
    	if ( balance >= 0 ) // if the balance is greater than or equals to 0
    	{
    		balance = account_balance;
    	}
    
    	else {
    		balance = 0;
    		cout << "\n INITIAL BALANCE WAS INVALID"
    			"\nBalance is set to 0";
    	}
    }
    
    void Account::credit( int add_amount )
    {
    	account_balance += add_amount;
    }
    
    void Account::debit( int withdraw_amount )
    {
    	if ( withdraw_amount < account_balance ) {
    		account_balance -= withdraw_amount;
    	}
    
    	else {
    		cout << "\nDebit amount exceeded account balance.";
    	}
    }
    
    int Account::getBalance()
    {
    	return account_balance;
    }
    and when i Compile I get these errors

    FILE: accountp.cpp LINE: 11 MESSAGE: error: no matching function for call to `Account::Account()'

    FILE: Account.h LINE: 9 MESSAGE:note: candidates are: Account::Account(const Account&)

    FILE: Account.h LINE: 11 MESSAGE:note: Account::Account(int)

    FILE: accountp.cpp LINE: 12 MESSAGE: error: no matching function for call to `Account::Account()'

    FILE: Account.h LINE: 9 MESSAGE: note: candidates are: Account::Account(const Account&)

    FILE: Account.h LINE: 11 MESSAGE:note: Account::Account(int)

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    22,165
    Basically, you use the default constructor but did not define it.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User
    Join Date
    Nov 2007
    Posts
    164
    how did I use the default constructor

    I defined my own constructor

    Code:
    Account( int balance ); // constructor

  4. #4
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    Quote Originally Posted by manzoor View Post
    how did I use the default constructor
    On line 11, according to the message. If I'm counting lines correctly, that was the line
    Code:
    Account account1;
    Since you only defined a constructor that takes an int, you need to supply an int rather than supply nothing at all.

  5. #5
    Registered User
    Join Date
    Nov 2007
    Posts
    164
    oh thanks lol

    so I should first get input from the user and than supply it to the constructor

    sorry sorry for that silly mistake

    and thanks thanks thanks for your help so soon thanks

  6. #6
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Posts
    22,915
    Code:
    	int amountw1, amountw2;
    
    	account1.debit(amountw1);
    	account1.debit(amountw2);
    Uninitialized variables used.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  7. #7
    Registered User
    Join Date
    Nov 2007
    Posts
    164
    oh yeah thanks for spotting this mistake too

    thanks

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. An error is driving me nuts!
    By ulillillia in forum C Programming
    Replies: 5
    Last Post: 04-04-2009, 10:15 PM
  2. Compiling sample DarkGDK Program
    By Phyxashun in forum Game Programming
    Replies: 6
    Last Post: 01-27-2009, 03:07 AM
  3. Getting other processes class names
    By Hawkin in forum Windows Programming
    Replies: 3
    Last Post: 03-20-2008, 05:02 PM
  4. C++ compilation issues
    By Rupan in forum C++ Programming
    Replies: 1
    Last Post: 08-22-2005, 06:45 AM
  5. Couple C questions :)
    By Divx in forum C Programming
    Replies: 5
    Last Post: 01-28-2003, 01:10 AM

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