Thread: class error

  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
    28,412
    Basically, you use the default constructor but did not define it.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    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,336
    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
    Location
    Inside my computer
    Posts
    24,654
    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, 09: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, 04:02 PM
  4. C++ compilation issues
    By Rupan in forum C++ Programming
    Replies: 1
    Last Post: 08-22-2005, 05:45 AM
  5. Couple C questions :)
    By Divx in forum C Programming
    Replies: 5
    Last Post: 01-28-2003, 01:10 AM