Thread: PLEASE HELP!! error C2228

  1. #16
    -bleh-
    Join Date
    Aug 2010
    Location
    somewhere in this universe
    Posts
    463
    Code:
    int main()
    {
    return 0;
    }
    two main functions and you compile without a problem?
    "All that we see or seem
    Is but a dream within a dream." - Poe

  2. #17
    Registered User
    Join Date
    Mar 2013
    Posts
    17
    Quote Originally Posted by nimitzhunter View Post
    Code:
    void BankAccount::computeInterest()
    {
       interest = interestRate(balance);<---- interstRate is not a function, it's a variable. 
       balance += interest;
    }
    i did that cause i thought the * symbol was causing it to think balance variable was a pointer. i changed it back to "interest = interestRate * balance" and i still get this error

  3. #18
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,377
    Quote Originally Posted by 5.5.7. Will
    i'm getting the same error for some reason. the only error there
    Now you commented out the inclusion of the header file, from what I see from your image. By doing that, the definition of the BankAccount class will not be present in that source file (translation unit).

    Oh, and don't post an image. Post your updated code in code tags; post your error message in full (in code tags too, if you will).

    Quote Originally Posted by 5.5.7. Will
    i did that cause i thought the * symbol was causing it to think balance variable was a pointer. i changed it back to "interest = interestRate * balance" and i still get this error
    Your code in that file did not even get compiled (fully) because you had a header inclusion guard that did not belong there.

    Quote Originally Posted by nimitzhunter
    two main functions and you compile without a problem?
    See above.
    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

  4. #19
    Registered User
    Join Date
    Mar 2013
    Posts
    17
    Quote Originally Posted by nimitzhunter View Post
    Code:
    int main()
    {
    return 0;
    }
    two main functions and you compile without a problem?
    i removed that just now and this error still pops up. i don't know what this is. no matter what i delete or change, it still shows this error.

  5. #20
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,377
    Quote Originally Posted by 5.5.7. Will
    i removed that just now and this error still pops up. i don't know what this is. no matter what i delete or change, it still shows this error.
    So post your updated code already. Just because you said that you did something doesn't mean you did it correctly.

    EDIT:
    And make sure that your updated code that you post is the one that results in the error message that you shall also post in full. No commenting out until that is what you compiled.
    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

  6. #21
    Registered User
    Join Date
    Mar 2013
    Posts
    17
    Quote Originally Posted by laserlight View Post
    Now you commented out the inclusion of the header file, from what I see from your image. By doing that, the definition of the BankAccount class will not be present in that source file (translation unit).

    Oh, and don't post an image. Post your updated code in code tags; post your error message in full (in code tags too, if you will).


    Your code in that file did not even get compiled (fully) because you had a header inclusion guard that did not belong there.
    Code:
    #include <iostream>using namespace std;
    
    
    class BankAccount
    {
       private:
          double balance; // holds the current account balance
    	  double interestRate; // holds the interest rate for the current period
    	  double interest; // holds the interest earned for the current period
    	  int numTransactions; // holds the current number of transactions
       public:
          BankAccount();
          void setInterestRate(double);
    	  void makeDeposit(double); 
    	  bool makeWithdraw(double); 
    	  void computeInterest();
    	  double getInterestRate();
    	  double getBalance();
    	  double getInterest();
    	  int getNumTransactions();
    };
    Code:
    #include <iostream>#include "BankAccount.h"
    using namespace std;
    
    
    BankAccount::BankAccount()
    {
       balance = 0;
       interestRate = 0.056;
       interest = 0;
       numTransactions = 0;
    }
    void BankAccount::setInterestRate(double interestRate)
    {
    	this->interestRate = interestRate;
    }
    void BankAccount::makeDeposit(double deposit)
    {
       balance += deposit;
       numTransactions++;
    }
    bool BankAccount::makeWithdraw(double withdraw)
    {
       if (balance < withdraw)
          return false;
       else
          {
             balance -= withdraw;
             numTransactions++;
             return true;
          }
    }
    void BankAccount::computeInterest()
    {
       interest = interestRate * balance;
       balance += interest;
    }
    double BankAccount::getInterestRate()
    {
       return interestRate;
    }
    double BankAccount::getBalance()
    {
       return balance;
    }
    double BankAccount::getInterest()
    {
       return interest;
    }
    int BankAccount::getNumTransactions()
    {
       return numTransactions;
    }
    Code:
    #include <iostream>#include "BankAccount.h"
    using namespace std;
    
    
    void heading();
    void displayMenu();  
    void makeDeposit(BankAccount &depositAmount);  
    void makeWithdraw(BankAccount &withdrawAmount);
    
    
    int main()
    {
       heading();
       cout << endl;
       displayMenu();
    
    
       BankAccount savings;
    
    
       int userChoice;
       cin >> userChoice;        
       for (userChoice; userChoice > 6;)
          {
    	     cout << endl;
    		 cout << "There is no option " << userChoice << ". Please choose a task that is listed." << endl;
    		 cout << endl;
    		 displayMenu();
             cin >> userChoice;
           }
        if (userChoice == 6)
          {
    	   cout << endl;
           cout << "Thank you. Please wait." << endl;
    	   cout << endl;
    	   cout << "Thank you for being a loyal customer!" << endl;
    	   cout << endl;
          }
       if (userChoice == 1)
          {
    		 BankAccount::makeDeposit(savings);
          }
       if (userChoice == 2)
          {
             BankAccount::makeWithdraw(savings);
          }
       if (userChoice == 3)
          {
             savings.getBalance();
          }
       if (userChoice == 4)
          {
             savings.getNumTransactions();
          }
       if (userChoice == 5)
          {
             savings.getInterest();
          }
    }
    void heading()
    {
        cout << "/********************************************************************************************" << endl;
     	cout << "*         Course Name: CMPS 191 – Modular Programming and Algorithms Development II" << endl;
    	cout << "*                                           Project 2" << endl;
    	cout << "* Section: 01" << endl;
    	cout << "* Semester: Spring 2013" << endl;
    	cout << "*" << endl;
    	cout << "* Project Members:" << endl;  
    	cout << "*        Leader: William R. Veal III" << endl;
    	cout << "*" << endl;  
        cout << "*" << endl;
     	cout << "* Instructor: Dr. Mathieu Kourouma" << endl;
    	cout << "*" << endl;
     	cout << "* Description:" << endl;
        cout << "*	Provide here a brief description of the purpose of the project" << endl;
    	cout << "*" << endl;
    	cout << "* Due Date: Wednesday, March 26, 2013 by 11:59 PM on Blackboard" << endl;
    	cout << "*" << endl;
     	cout << "* Certificate of Authentication:" << endl;
    	cout << "*" << endl;
    	cout << "* We, the listed group members:" << endl;
    	cout << "*	 Claim that this project’s work fully our own work." << endl;
    	cout << "*	 We wrote this program independently." << endl; 
       	cout << "*	 We did not copy any part of the code from other group members." << endl;
       	cout << "*	 We did not share at all our codes with other group members." << endl;
    	cout << "* 	 We are only allowed to seek help from the instructor of this course and group members" << endl;
       	cout << "*" << endl; 
       	cout << "* Signature: WRVIII" << endl;
    	cout << "*" << endl;
     	cout << "**********************************************************************************************/" << endl;
    }
    void displayMenu()
    {
       cout << "----------------------------------------------------------------------------------------------------------------------------------" << endl;
       cout << "					                   Welcome to SUBR's Credit Union Banking System" << endl;
       cout << "-----------------------------------------------------------------------------------------------------------------------------------" << endl;
       cout << "" << endl;
       cout << "Please select from the following menu:" << endl;
       cout << " " << endl;
       cout << "1.	Make a deposit" << endl;
       cout << "2.	Make a withdraw" << endl;
       cout << "3.	Display account balance" << endl;
       cout << "4.	Display the number of transactions" << endl;
       cout << "5.	Display interest earned " << endl;
       cout << "6.	Exit the banking system" << endl;
       cout << " " << endl;
       cout << "Make your selection: ";
    }
    void makeDeposit(BankAccount &depositAmount)
    {
      double deposit;
    
    
      cout << "Enter the amount you wish to deposit: ";
      cin >> deposit;
      cout << endl;
      depositAmount.makeDeposit(deposit);
      cout << "Your deposit is " << deposit << endl;
    }
    void makeWithdraw(BankAccount &withdrawAmount)
    {
       double withdraw;
     
       cout << "Enter the amount you wish to withdraw: ";
       cin >> withdraw;
       if (!withdrawAmount.makeWithdraw(withdraw))
          cout << "The amount you want to withraw is larger than the balance." << endl;
    }
    ------ Build started: Project: BankAccount.h, Configuration: Debug Win32 ------
    Compiling...
    TestBankAccount.cpp
    c:\users\william\documents\visual studio 2008\projects\bankaccount.h\bankaccount.h\testbank account.cpp(38) : error C2228: left of '.getBalance' must have class/struct/union

  7. #22
    -bleh-
    Join Date
    Aug 2010
    Location
    somewhere in this universe
    Posts
    463
    Code:
       if (userChoice == 1)
          {
             BankAccount::makeDeposit(savings);
          }
       if (userChoice == 2)
          {
             BankAccount::makeWithdraw(savings);
          }


    EDIT: you have the functions
    Code:
    void makeDeposit(BankAccount &depositAmount); 
    void makeWithdraw(BankAccount &withdrawAmount);
    No such thing as BankAccount::makeWithdraw(Bankaccount &)...... fixing these and those code compiles just fine with g++. Don't see any problem with getBalance.
    "All that we see or seem
    Is but a dream within a dream." - Poe

  8. #23
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,377
    Ah. Notice that you use nothing from <iostream> to define your BankAccount class. Therefore, I expect your header to be like:
    Code:
    #ifndef BANKACCOUNT_H
    #define BANKACCOUNT_H
    
    class BankAccount
    {
    public:
        BankAccount();
        void setInterestRate(double);
        void makeDeposit(double);
        bool makeWithdraw(double);
        void computeInterest();
        double getInterestRate();
        double getBalance();
        double getInterest();
        int getNumTransactions();
    private:
        double balance; // holds the current account balance
        double interestRate; // holds the interest rate for the current period
        double interest; // holds the interest earned for the current period
        int numTransactions; // holds the current number of transactions
    };
    
    #endif
    Notice that I use header inclusion guards because it is a header. Putting public before private is just a matter of common C++ style that you don't have to follow.

    Now, concerning this:
    Code:
    #include <iostream>#include "BankAccount.h"
    Break it up into:
    Code:
    #include <iostream>
    #include "BankAccount.h"
    for both source files. Compile and see what you get (i.e., you should get other error messages).
    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

  9. #24
    Registered User
    Join Date
    Mar 2013
    Posts
    17
    Quote Originally Posted by nimitzhunter View Post
    Code:
       if (userChoice == 1)
          {
             BankAccount::makeDeposit(savings);
          }
       if (userChoice == 2)
          {
             BankAccount::makeWithdraw(savings);
          }


    EDIT: you have the functions
    Code:
    void makeDeposit(BankAccount &depositAmount); 
    void makeWithdraw(BankAccount &withdrawAmount);
    No such thing as BankAccount::makeWithdraw(Bankaccount &)...... fixing these and those code compiles just fine with g++. Don't see any problem with getBalance.
    you got it to compile ?

    wtf.

    how ?

  10. #25
    -bleh-
    Join Date
    Aug 2010
    Location
    somewhere in this universe
    Posts
    463
    "All that we see or seem
    Is but a dream within a dream." - Poe

  11. #26
    Registered User
    Join Date
    Mar 2013
    Posts
    17
    Quote Originally Posted by laserlight View Post
    Ah. Notice that you use nothing from <iostream> to define your BankAccount class. Therefore, I expect your header to be like:
    Code:
    #ifndef BANKACCOUNT_H
    #define BANKACCOUNT_H
    
    class BankAccount
    {
    public:
        BankAccount();
        void setInterestRate(double);
        void makeDeposit(double);
        bool makeWithdraw(double);
        void computeInterest();
        double getInterestRate();
        double getBalance();
        double getInterest();
        int getNumTransactions();
    private:
        double balance; // holds the current account balance
        double interestRate; // holds the interest rate for the current period
        double interest; // holds the interest earned for the current period
        int numTransactions; // holds the current number of transactions
    };
    
    #endif
    Notice that I use header inclusion guards because it is a header. Putting public before private is just a matter of common C++ style that you don't have to follow.

    Now, concerning this:
    Code:
    #include <iostream>#include "BankAccount.h"
    Break it up into:
    Code:
    #include <iostream>
    #include "BankAccount.h"
    for both source files. Compile and see what you get (i.e., you should get other error messages).
    Code:
    #ifndef BANKACCOUNT_H
    #define BANKACCOUNT_H
    
    
    class BankAccount
    {
       public:
        BankAccount();
        void setInterestRate(double);
        void makeDeposit(double);
        bool makeWithdraw(double);
        void computeInterest();
        double getInterestRate();
        double getBalance();
        double getInterest();
        int getNumTransactions();
       private:
        double balance; // holds the current account balance
        double interestRate; // holds the interest rate for the current period
        double interest; // holds the interest earned for the current period
        int numTransactions; // holds the current number of transactions
    };
    #endif
    Code:
    #include <iostream>
    #include "BankAccount.h"
    using namespace std;
    
    
    BankAccount::BankAccount()
    {
       balance = 0;
       interestRate = 0.056;
       interest = 0;
       numTransactions = 0;
    }
    void BankAccount::setInterestRate(double interestRate)
    {
        this->interestRate = interestRate;
    }
    void BankAccount::makeDeposit(double deposit)
    {
       balance += deposit;
       numTransactions++;
    }
    bool BankAccount::makeWithdraw(double withdraw)
    {
       if (balance < withdraw)
          return false;
       else
          {
             balance -= withdraw;
             numTransactions++;
             return true;
          }
    }
    void BankAccount::computeInterest()
    {
       interest = interestRate * balance;
       balance += interest;
    }
    double BankAccount::getInterestRate()
    {
       return interestRate;
    }
    double BankAccount::getBalance()
    {
       return balance;
    }
    double BankAccount::getInterest()
    {
       return interest;
    }
    int BankAccount::getNumTransactions()
    {
       return numTransactions;
    }
    Code:
    #include <iostream>
    #include "BankAccount.h"
    using namespace std;
    
    
    void heading();
    void displayMenu();  
    void makeDeposit(BankAccount &depositAmount);  
    void makeWithdraw(BankAccount &withdrawAmount);
    
    
    int main()
    {
       heading();
       cout << endl;
       displayMenu();
    
    
       BankAccount savings;
    
    
       int userChoice;
       cin >> userChoice;        
       for (userChoice; userChoice > 6;)
          {
             cout << endl;
             cout << "There is no option " << userChoice << ". Please choose a task that is listed." << endl;
             cout << endl;
             displayMenu();
             cin >> userChoice;
           }
        if (userChoice == 6)
          {
           cout << endl;
           cout << "Thank you. Please wait." << endl;
           cout << endl;
           cout << "Thank you for being a loyal customer!" << endl;
           cout << endl;
          }
       if (userChoice == 1)
          {
             BankAccount::makeDeposit(savings);
          }
       if (userChoice == 2)
          {
             BankAccount::makeWithdraw(savings);
          }
       if (userChoice == 3)
          {
             savings.getBalance();
          }
       if (userChoice == 4)
          {
             savings.getNumTransactions();
          }
       if (userChoice == 5)
          {
             savings.getInterest();
          }
    }
    void heading()
    {
        cout << "/********************************************************************************************" << endl;
         cout << "*         Course Name: CMPS 191 – Modular Programming and Algorithms Development II" << endl;
        cout << "*                                           Project 2" << endl;
        cout << "* Section: 01" << endl;
        cout << "* Semester: Spring 2013" << endl;
        cout << "*" << endl;
        cout << "* Project Members:" << endl;  
        cout << "*        Leader: William R. Veal III" << endl;
        cout << "*" << endl;  
        cout << "*" << endl;
         cout << "* Instructor: Dr. Mathieu Kourouma" << endl;
        cout << "*" << endl;
         cout << "* Description:" << endl;
        cout << "*    Provide here a brief description of the purpose of the project" << endl;
        cout << "*" << endl;
        cout << "* Due Date: Wednesday, March 26, 2013 by 11:59 PM on Blackboard" << endl;
        cout << "*" << endl;
         cout << "* Certificate of Authentication:" << endl;
        cout << "*" << endl;
        cout << "* We, the listed group members:" << endl;
        cout << "*     Claim that this project’s work fully our own work." << endl;
        cout << "*     We wrote this program independently." << endl; 
           cout << "*     We did not copy any part of the code from other group members." << endl;
           cout << "*     We did not share at all our codes with other group members." << endl;
        cout << "*      We are only allowed to seek help from the instructor of this course and group members" << endl;
           cout << "*" << endl; 
           cout << "* Signature: WRVIII" << endl;
        cout << "*" << endl;
         cout << "**********************************************************************************************/" << endl;
    }
    void displayMenu()
    {
       cout << "----------------------------------------------------------------------------------------------------------------------------------" << endl;
       cout << "                                       Welcome to SUBR's Credit Union Banking System" << endl;
       cout << "-----------------------------------------------------------------------------------------------------------------------------------" << endl;
       cout << "" << endl;
       cout << "Please select from the following menu:" << endl;
       cout << " " << endl;
       cout << "1.    Make a deposit" << endl;
       cout << "2.    Make a withdraw" << endl;
       cout << "3.    Display account balance" << endl;
       cout << "4.    Display the number of transactions" << endl;
       cout << "5.    Display interest earned " << endl;
       cout << "6.    Exit the banking system" << endl;
       cout << " " << endl;
       cout << "Make your selection: ";
    }
    void makeDeposit(BankAccount &depositAmount)
    {
      double deposit;
    
    
      cout << "Enter the amount you wish to deposit: ";
      cin >> deposit;
      cout << endl;
      depositAmount.makeDeposit(deposit);
      cout << "Your deposit is " << deposit << endl;
    }
    void makeWithdraw(BankAccount &withdrawAmount)
    {
       double withdraw;
     
       cout << "Enter the amount you wish to withdraw: ";
       cin >> withdraw;
       if (!withdrawAmount.makeWithdraw(withdraw))
          cout << "The amount you want to withraw is larger than the balance." << endl;
    }
    i got the same exact error.

  12. #27
    -bleh-
    Join Date
    Aug 2010
    Location
    somewhere in this universe
    Posts
    463
    Code:
    if (userChoice == 1)
          {
             BankAccount::makeDeposit(savings);
          }
       if (userChoice == 2)
          {
             BankAccount::makeWithdraw(savings);
          }
    can you change these function calls first?
    "All that we see or seem
    Is but a dream within a dream." - Poe

  13. #28
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,377
    Quote Originally Posted by 5.5.7. Will
    i got the same exact error.
    You still have the two include directives on the same line. The problem is that this:
    Code:
    #include <iostream>#include "BankAccount.h"
    could be interpreted as:
    Code:
    #include <iostream>
    with some rubbish characters after that. Therefore, you are not actually including BankAccount.h. If you compile at a higher warning level, your compiler might warn you about this. To fix this, it must be:
    Code:
    #include <iostream>
    #include "BankAccount.h"
    EDIT:
    You seem to have the same problem here:
    Code:
    #ifndef BANKACCOUNT_H#define BANKACCOUNT_H
    is there something wrong that you keep writing these preprocessor directives on the same line?
    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

  14. #29
    Registered User
    Join Date
    May 2009
    Posts
    4,092
    Quote Originally Posted by 5.5.7. Will View Post
    you got it to compile ?

    wtf.

    how ?
    Likely like I did; use a good compiler fix the warnings and errors.

    Code:
       if (userChoice == 1)
          {
             /* BankAccount:: */ makeDeposit(savings);
          }
       if (userChoice == 2)
          {
             /* BankAccount:: */ makeWithdraw(savings);
          }
    I also did most of the things that laserlight suggested.

    Tim S.
    "...a computer is a stupid machine with the ability to do incredibly smart things, while computer programmers are smart people with the ability to do incredibly stupid things. They are,in short, a perfect match.." Bill Bryson

  15. #30
    Registered User
    Join Date
    Mar 2013
    Posts
    17
    Quote Originally Posted by laserlight View Post
    You still have the two include directives on the same line. The problem is that this:
    Code:
    #include <iostream>#include "BankAccount.h"
    could be interpreted as:
    Code:
    #include <iostream>
    with some rubbish characters after that. Therefore, you are not actually including BankAccount.h. If you compile at a higher warning level, your compiler might warn you about this. To fix this, it must be:
    Code:
    #include <iostream>
    #include "BankAccount.h"
    EDIT:
    You seem to have the same problem here:
    Code:
    #ifndef BANKACCOUNT_H#define BANKACCOUNT_H
    is there something wrong that you keep writing these preprocessor directives on the same line?
    there on the same line because of the [ code ] thing on this forum. it's just fine on my compiler. that's not the problem

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 6
    Last Post: 10-29-2012, 03:33 AM
  2. Error C2109 + C2228
    By deeisenberg in forum C Programming
    Replies: 8
    Last Post: 04-23-2012, 11:04 AM
  3. Replies: 4
    Last Post: 07-24-2011, 09:38 PM
  4. Replies: 3
    Last Post: 10-02-2007, 09:12 PM
  5. Compiler error c2228
    By <showMe> in forum C++ Programming
    Replies: 2
    Last Post: 06-28-2006, 08:31 PM