Thread: Multiple source files for one program

  1. #1
    Some Guy
    Join Date
    Jul 2004
    Posts
    32

    Question Multiple source files for one program

    I've searched and read some of the topics. I've read this. and it's still not working. I can't seem to split up my program into 3 pieces. I've managed to split the program into 2, seperating the header and the class functions/main. But once I seperate main from the class functions, it says Linker Error. I've just about given up. I'm wondering if it just my problem. Is someone could please help, that would great. Here is my simple program (there's no comments).

    //Bank.h
    Code:
    #ifndef BANK_H  
    #define BANK_H
    class Bank
    {
    	public:
    		Bank() { balance = 1000; transactions = 0; interest = .15;}
    		Bank(float itsBalance)
    			{ balance = itsBalance;	transactions = 0; interest = .15;}
    		~Bank() {std::cout << "Destructor called";}
    		void deposit(float depAmount) 
      			{ balance += depAmount; transactions++; }
    		void withdraw(float wdrawAmount) 
      			{ balance -= wdrawAmount; transactions++; }
    		float getInterest() { return interest * balance; }
    		float getBalance() { return balance; }
    		int getTransactions() { return transactions; }
    		void displayMenu();
    	private:
    		float balance;
    		int transactions;
    		float interest;
    };
    #endif

    // Bank.cpp
    Code:
    #include <iostream>
    #include <conio.h>
    #include "Bank.h"
    
    using namespace std;
    
    
    void Bank::displayMenu()
    {
    	cout << "\n\n1: View Balance\n"
    		 << "2: Deposit money\n"
    		 << "3: Withdraw\n"
    		 << "4: View Interest\n"
    		 << "5: View Transactions\n"
    		 << "6: Exit\n\n";
    }
    // main.cpp
    Code:
    #include <iostream>
    #include <conio.h>
    #include "Bank.h"
    
    using namespace std;
    
    
    int main()
    {
    	Bank account(5000);
    	int choice, amount;
    	while(1)
    	{
    		account.displayMenu();
        	cin >> choice;
        switch(choice)
       	{
       		case 1:	cout << "You're balance is $" << account.getBalance(); break;
       		case 2: cout << "How much money would you like to deposit?\n";
    	   			cin >> amount; account.deposit(amount); break;
    	    case 3: cout << "How much money would you like to withdraw?\n";
    	   			cin >> amount; 
           			if(amount > account.getBalance())
          				{ cout << "Sorry, you don't have enough money in the bank\n"; break;}
               		account.withdraw(amount); break;	   			
            case 4: cout << "Your interest is $" << account.getInterest() << endl; break;
            case 5: cout << "You've made " << account.getTransactions() 
            			 << " transactions.\n"; break;
            case 6: exit(0);
            default: cout << "Please enter a valid number!";
        }
        }// while (choice !=6);
        getch();
        return 0;
    }

  2. #2
    Sweet
    Join Date
    Aug 2002
    Location
    Tucson, Arizona
    Posts
    1,820
    hmmmm seems to work fine for me what are your linker errors
    Woop?

  3. #3
    Some Guy
    Join Date
    Jul 2004
    Posts
    32
    Ok, so it is just me!

    Compiler: Default compiler
    Executing g++.exe...
    g++.exe "C:\Program Files\Dev-Cpp\main.cpp" -o "C:\Program Files\Dev-Cpp\main.exe" -fsave-memoized -g3 -I"C:\Program Files\Dev-Cpp\include\c++" -I"C:\Program Files\Dev-Cpp\include\c++\mingw32" -I"C:\Program Files\Dev-Cpp\include\c++\backward" -I"C:\Program Files\Dev-Cpp\include" -L"C:\Program Files\Dev-Cpp\lib"
    C:\DOCUME~1\GABEFL~1\LOCALS~1\Temp/ccaOaaaa.o(.text+0x159): In function `main':
    C:/Program Files/Dev-Cpp/main.cpp:14: undefined reference to `Bank::displayMenu()'

    Execution terminated
    That's what it says. Could it be where my files are saved? They are in C:\Program Files\Dev-Cpp\My Programs
    Alright, I'm going to sleep. I'm exhausted. Thanks for the quick reply. At least I now know I'm doing it correctly.

  4. #4
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    Add bank.cpp to the project
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Program Deployment and DLL/OCX Files?
    By dfghjk in forum C++ Programming
    Replies: 5
    Last Post: 06-16-2008, 02:47 AM
  2. Working with multiple source files
    By abh!shek in forum C Programming
    Replies: 17
    Last Post: 06-03-2008, 11:23 AM
  3. Get program to copy itself into program files, then start on startup.
    By guitarist809 in forum Windows Programming
    Replies: 6
    Last Post: 03-03-2008, 09:42 AM
  4. Linking header files, Source files and main program(Accel. C++)
    By Daniel Primed in forum C++ Programming
    Replies: 3
    Last Post: 01-17-2006, 11:46 AM
  5. Need help with input streams from multiple source files
    By orikon in forum C++ Programming
    Replies: 2
    Last Post: 10-08-2005, 02:56 PM