Thread: Preprocessor issues

  1. #1
    Registered User
    Join Date
    Oct 2009
    Posts
    3

    Preprocessor issues

    I am having severe issues with preprocessor commands.

    I have 3 files
    • account.h
    • account.cpp
    • bank.cpp


    bank.cpp makes use of account as a class.
    I just end up with a ton of errors every time I compile (they seem like they could fill a post on their own)

    Here is account.h
    Code:
    #ifndef _Account_
    #define Account
    
    class Account
    {
      double money;                      // amount of money held by this account
      double interestRate;  // a monthly or yearly interestrate, depending on how the account is to be used.
    
    
    public:
    
         // create an account with an initial amountand a specified interest rate
    
    	Account(double amount, double percent);
    
    
         // return the account's balance
      double balance();
         // add money to the account
      void deposit(double);
         // substract money from the account
      void withdraw(double);
         // add money according to the interest rate.
      void addInterest();
    
    };
    #endif
    Here is account.cpp
    Code:
    #include "account.h"
    
    
    
    		     // create an account with an initial amountand a specified interest rate
    		  Account(double amount, double percent)
    		  {
    			money = amount;
    			interestRate = percent;
    		  }
    
    			 // return the account's balance
    		  double balance()
    		  {
    			return money;
    		  }
    
    			 // add money to the account
    		  void deposit(double amount)
    		  {
    			money += amount;
    		  }
    
    			 // substract money from the account
    		  void withdraw(double amount)
    		  {
    			money -= amount;
    		  }
    
    			 // add money according to the interest rate.
    		  void addInterest()
    		  {
    			money *= (1 + interestRate/100.0);
    				  // this, ofcourse, is the same as money = money * (1 + (interestRate/100) );
    		  }
    And here is bank.cpp
    Code:
    #include <iostream>
    #include "account.cpp"
    
    using namespace std;
    
    
    	void main(void)
    	{
    		Account stdAccount(100, 4); // create an account with £100and 4% interest rate
    
    	  stdAccount.addInterest();
    	  cout << "Balance: " << stdAccount.balance()<< endl;
    
    	  stdAccount.deposit(50);
    	  cout << "Balance: " << stdAccount.balance()<< endl;
    
    	  stdAccount.withdraw(100);
    	  cout << "Balance: " << stdAccount.balance()<< endl;
    
    	  int i;
    	  cin >> i;
    	}
    Any help with this issue would be really appreciated!

    The errors that appear to begin with are
    Error 1 error C2062: type 'double' unexpected d:\users\tom\documents\visual studio 2008\projects\bank\bank\account.h 14
    Error 3 error C2062: type 'double' unexpected d:\users\tom\documents\visual studio 2008\projects\bank\bank\account.cpp 6
    Error 11 error C2062: type 'double' unexpected d:\users\tom\documents\visual studio 2008\projects\bank\bank\account.h 14
    Error 13 error C2062: type 'double' unexpected d:\users\tom\documents\visual studio 2008\projects\bank\bank\account.cpp 6

    Thanks for reading my post

    /Sed

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    You never include .cpp files.

  3. #3
    Registered User
    Join Date
    Oct 2009
    Posts
    3
    thanks for pointing out that tab I feel like a right fool now! but learning is like that

    ok after modifying the top of my bank.cpp to look like this
    Code:
    #include <iostream>
    #include "account.h"
    
    using namespace std;
    using namespace account;
    it cuts the error count down to 23 and dumps these 4 out to start

    #include <iostream>
    #include "account.h"

    using namespace std;
    using namespace account;

    Thanks

  4. #4
    The larch
    Join Date
    May 2006
    Posts
    3,573
    There's also a problem here:

    Code:
    #ifndef _Account_
    #define Account
    You should define the same macro that isn't defined. And you shouldn't use identifiers beginning with underscore(s) (the precise rules are bit more complicated), since these are reserved for compiler / standard library implementations.

    And you shouldn't declare macros that collide with other names you want to use (macro Account and class Account). A popular way to ensure that macros don't collide with other identifiers is to use all caps for them. And for header guards you might also use a suffix like *_H or *_DEFINED.
    Last edited by anon; 10-11-2009 at 04:40 PM.
    I might be wrong.

    Thank you, anon. You sure know how to recognize different types of trees from quite a long way away.
    Quoted more than 1000 times (I hope).

  5. #5
    Registered User
    Join Date
    Oct 2009
    Posts
    3
    I fixed it by simply adjusting my preprocessor commands in account.h and adding private: before my variables

    see new account.h top
    Code:
    #ifndef Account_h
    #define Account_h
    
    class Account
    {
    private:
      double money;                      // amount of money held by this account
      double interestRate;  // a monthly or yearly interestrate, depending on how the account is to be used.
    
    
    public:
    
         // create an account with an initial amountand a specified interest rate
      Account(double amount, double percent);
    Thanks for all the help

    /Sed

Popular pages Recent additions subscribe to a feed