Thread: Need Help with Classes

  1. #1
    Registered User
    Join Date
    Mar 2010
    Posts
    19

    Need Help with Classes

    I'm writing a program called BankAccount that uses a class called BankAccount. I'm pretty sure I have everything right that I want to have in it, but when I try to compile it, it comes up with 13 errors. I'm pretty sure they all have to do with the first error that says : "Error C2065: 'BankAccount' : Undeclared identifier"
    I'm going to put in both my .h file and my tester file so that maybe you can help me see what is wrong with it? And why it's not recognizing the class that I made. Thanks so much

    Header file (BankAccount.h)

    Code:
    #include <iostream>
    #include <iomanip>
    #include <cmath>
    #include <string>
    
    using namespace std;
    
    class BankAccount
    {
    private:
    	int acctNumber;
    	string name;
    	double balance; 
    
    public:
    	BankAccount();
    	BankAccount(int, string, double);
    	void setAcctNumber(int);
    	void setName(string);
    	void setBalance(double); 
    	int getAcctNumber(); 
    	string getName(); 
    	double getBalance(); 
    	void deposit(double);
    	void withdraw(double);
    	void displayData();
    
    };
    
    BankAccount::BankAccount()
    {
    	acctNumber = 0;
    	name = "unknown";
    	balance = 0.0;
    }
    
    BankAccount::BankAccount(int aNum, string aName, double amount)
    {
    	acctNumber = aNum; 
    	name = aName; 
    	balance = amount;
    
    }
    
    void BankAccount::setAcctNumber(int aNum)
    {
    	acctNumber = aNum;
    }
    
    void BankAccount::setName(string aName)
    {
    	name = aName;
    }
    
    void BankAccount::setBalance(double amount)
    {
    	balance = amount;
    }
    
    int BankAccount::getAcctNumber()
    {
    	return acctNumber;
    }
    
    string BankAccount::getName()
    {
    	return name;
    }
    
    double BankAccount::getBalance()
    {
    	return balance;
    }
    
    void BankAccount::deposit(double amount)
    {
    	balance += amount;
    }
    
    void BankAccount::withdraw(double amount)
    {
    	balance -= amount;
    }
    
    void BankAccount::displayData();
    {
    	cout << "Your account number is " << acctNum << endl; 
    	cout << "Your name is " << name << endl; 
    	cout << "Your balance is " << balance << endl;
    }

    Tester file (Bank.cpp)

    Code:
    #include <iostream>
    #include <iomanip>
    #include <cmath>
    #include <string>
    
    using namespace std;
    
    void displayMenu();
    
    int main()
    {
    	BankAccount studentAccount; 
    	double amount = 0.0;
    	int choice = 0;
    	int iValue = 0;
    	string name = "Millionare";
    	
    	do {
    		displayMenu(); 
    	      cin >> choice;
    		cout << endl;
    		switch (choice)
    		{
    			case 1 : do
    					 {
    					 cout<< "Enter amount: (greater than 0) " ; 
    					 cin >> amount;
    					 studentAccount.deposit(amount);
    					 break;
    					 }
    					 while (amount <= 0);
    			case 2 : do
    					 {
    					 cout << "Enter amount: (greater than 0) " ; 
    					 cin >> amount;
    					 studentAccount.withdraw(amount);
    					 break;
    					 }
    					 while (amount <= 0);
    			case 3 : cout << "Enter the name: ";
    					 cin.ignore();  
    					 getline(cin,name); 
    					 studentAccount.setName(name);
    					 break;
    			case 4 : cout << "Enter the number: ";
    					 cin >> iValue;
    					 studentAccount.setAcctNumber(iValue);
    					 break;
    			case 5 : studentAccount.displayData(); 
    					 break;
    			case 6 : cout << "\nBank is closed.\n" ;
    					 break;
    			default : cout << "Invalid entry.";
    		}
    
    	}while (choice != 6);
    	
    
    	return 0;
    
    }
    
    void displayMenu()
    {
    	cout << "\n         Welcome to your bank \n\n";
    	cout << "1. Make a deposit\n";
    	cout << "2. Make a withdrawal\n";
    	cout << "3. Set the account name\n";
    	cout << "4. Set the account number\n";
    	cout << "5. Display account information\n";
    	cout << "6. Exit\n\n";
    	cout << "Please enter your choice: ";
    }

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    You need to include your header in your .cpp file. (Also your header should typically just have declarations in it -- the actual functions would typically go in another .cpp file that does not get included, but compiled separately and linked together with your main program.)

  3. #3
    Registered User
    Join Date
    Aug 2003
    Posts
    1,218
    You need to include BankAccount.h in Bank.cpp with a
    #include "BankAccount.h"
    statement

    On another note, you should seperate the declaration and the definition of class members into a .h and a .cpp file sperarately. So your files could be something like

    BankAccount.h, contains the class part:
    Code:
    class BankAccount
    {
        yadda yadda yadda
    };
    BankAccount.cpp, includes BankAccount.h and any other header needed and defines the member functions of the class BankAccount
    Bank.cpp, defines the main program just like you have it now, also includes BankAccount.h

  4. #4
    Registered User
    Join Date
    Mar 2010
    Posts
    19
    That's weird. It's not how our teacher told us to do it.
    We did it just like this in class and somehow it worked.

  5. #5
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Your teacher definitely told you to #include the header file. (edit unless of course, s/he put everything into one big file.)

  6. #6
    Registered User
    Join Date
    Mar 2010
    Posts
    68
    Just do the #include "BankAccount.h" in your .ccp file and you will be good. Putting the function definitions in a separate .cpp file will come later in your class.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Can you Initialize all classes once with New?
    By peacerosetx in forum C++ Programming
    Replies: 12
    Last Post: 07-02-2008, 10:47 AM
  2. im extreamly new help
    By rigo305 in forum C++ Programming
    Replies: 27
    Last Post: 04-23-2004, 11:22 PM
  3. Exporting VC++ classes for use with VB
    By Helix in forum Windows Programming
    Replies: 2
    Last Post: 12-29-2003, 05:38 PM
  4. Prime Number Generator... Help !?!!
    By Halo in forum C++ Programming
    Replies: 9
    Last Post: 10-20-2003, 07:26 PM
  5. include question
    By Wanted420 in forum C++ Programming
    Replies: 8
    Last Post: 10-17-2003, 03:49 AM