Page 1 of 4 123 ... LastLast
Results 1 to 10 of 31

Thread: Why am i getting a invalid use of Class?

  1. #1
    Join Date
    Sep 2005
    Posts
    2,394
    Rep Power
    0

    Default Why am i getting a invalid use of Class?

    Working on an assignment and keeping getting the following error at the 2 points in bold that call the class Account(int)

    Code:
    main.cpp:67: error: invalid use of `class Account'
    Code:
    #include <iostream>
    
    using namespace std;
    
    /*
     * 
     */
    
    int choice;
    int choice2;
    
    class Account {
        
    private:
        int balance;
        
    public:
        Account(){
            balance = 0;
        }
            
        Account(int a);
        int creditAccount();
        int debitAccount();
        void getBalance();
    };
    
    void menu();
    void submenu();
    
    int main(int argc, char** argv) {
        
        int amount;
        Account user1;
        Account user2;
        
        do {
            
            menu();
    
            cout << "Choice: ";
            
            cin >> choice;
            
            switch (choice) {
                
                case 1:
                    
                    submenu();
                    cout << "Choice: ";
                    cin >> choice2;
                    
                    switch (choice2){
                        
                        case 1:
                            
                            cout << "Please Enter Opening Balance amount";
                            cin >> amount;
                            user1.Account(amount);
                            break;
                            
                        case 2:
                            
                            cout << "Please Enter Opening Balance amount";
                            cin >> amount;
                            user2.Account(amount);
                            break;
                            
                        case 9:
                            break;
                    }
                    break;
                    
                case 2:
                    
                    submenu();
                    cout << "Choice: ";
                    cin >> choice2;
                    
                    switch (choice2){
                        
                        case 1:
                            
                            user1.getBalance();
                            break;
                            
                        case 2:
                            
                            user2.getBalance();
                            break;
                            
                        case 9:
                            break;
                    }
                    break;
                    
                case 3:
                    
                    submenu();
                    cout << "Choice: ";
                    cin >> choice2;
                    
                    switch (choice2){
                        
                        case 1:
                            
                            cout << "Please Enter Lodgement amount";
                            cin >> amount;
                            user1.creditAccount();
                            break;
                            
                        case 2:
                            
                            cout << "Please Enter Lodgement amount";
                            cin >> amount;
                            user2.creditAccount();
                            break;
                            
                        case 9:
                            break;
                    }
                    break;
                    
                case 4:
                    
                    
                    
                case 9:
                    
                    cout << "Thanks for Processing! Have a good Day!";
                    break;
                    
                default:
                    
                    cout << "Invalid Option Selected. Please Try again!";
                    break;
                    cout << "Choice: ";
                    cin >> choice;
                
            }
                    
            
        }while (choice != 9);
    
        return 0;
    }
    
    void menu(){
        
        cout << "###############################################################" << endl << 
                "##                  Account Transaction Menu                 ##" << endl <<
                "##                                                           ##" << endl <<
                "##                                                           ##" << endl <<
                "##   Please select the number for the option needed:         ##" << endl <<
                "##                                                           ##" << endl <<
                "##   1. Enter Account Opening Balance                        ##" << endl <<
                "##   2. Check Account Balance                                ##" << endl <<
                "##   3. Make Account Lodgement                               ##" << endl <<
                "##   4. Make Withdrawal from Account                         ##" << endl <<
                "##   9. Exit                                                 ##" << endl <<
                "##                                                           ##" << endl <<
                "###############################################################" << endl;
    }
    
    void submenu(){
        
        cout << "###############################################################" << endl << 
                "##                  Account Transaction Menu                 ##" << endl <<
                "##                                                           ##" << endl <<
                "##                                                           ##" << endl <<
                "##   Please select the number for the option needed:         ##" << endl <<
                "##                                                           ##" << endl <<
                "##   1. User1                                                ##" << endl <<
                "##   2. User2                                                ##" << endl <<
                "##   9. Back to Main                                         ##" << endl <<
                "##                                                           ##" << endl <<
                "###############################################################" << endl;
    }
    
    Account::Account(int a){
        
        if (a >= 0){
            
            balance = a;
        }
        else {
            
            cout << "Invalid Amount Entered";
            balance = 0;
        }
    }
    
    int Account::creditAccount(){
        
        
    }
    
    int Account::debitAccount(){
        
        
    }
    
    void Account::getBalance(){
        
        cout << "The current available balance is: " << balance << endl << endl;
        cin.get();
    }
    To find what you seek in the road of life, the best proverb of all is that which says: "Leave no stone unturned." Edward Bulwer Lytton

  2. #2
    Join Date
    Apr 2003
    Posts
    13,164
    Rep Power
    29

    Default

    You can't call a class constructor explicitly like that. Constructors (both primary and default) are only called when you first initialize (or create) a variable.

    So to use your Account() and Account(int a) functions, you would have to use it like this:
    Code:
    Account user1;  //Calls Default constructor
    Account user1(0);  //Calls Primary constructor
    If you want to use it, the way you are doing (user.Account(amount)) you will need to create a mutator function, like this:
    Code:
    void Account::setBalance(int a){
        if (a >= 0){
    
            balance = a;
        }
        else {
    
            cout << "Invalid Amount Entered";
            balance = 0;
        }
    }
    And refer to it, from your main like so:
    Code:
    user1.setBalance(amount);
    "The best software is the one that fits your needs." - A_A

    Virus free since: date unknown
    Anti-virus free since: August 2009

  3. #3
    Join Date
    Sep 2005
    Posts
    2,394
    Rep Power
    0

    Default

    Oh? hmmmm. Ok here is the portion of the question that that section of the program should be addressing:
    Your class should provide a constructor that receives an initial balance and uses it to initialize the data member. The constructor should validate the initial balance to ensure that it is greater than or equal to 0. If not, the balance should be set to 0 and the constructor should display an error message, indicating that the initial balance was invalid.
    Will using the mutator fulfill that requirement seeing that it says the constructor?
    To find what you seek in the road of life, the best proverb of all is that which says: "Leave no stone unturned." Edward Bulwer Lytton

  4. #4
    Join Date
    Apr 2003
    Posts
    13,164
    Rep Power
    29

    Default

    Quote Originally Posted by lovepython View Post
    Oh? hmmmm. Ok here is the portion of the question that that section of the program should be addressing:

    Will using the mutator fulfill that requirement seeing that it says the constructor?
    Constructors are only executed when you first create your variable.
    It didn't say you are to get the initial balance from the user.

    So it seems to me, from my understanding of it, that you have 2 options:
    1. Pass the initial balance, when you create the variable. Since it is a bank account of sorts, could set it to 500. And then allow the user to set their own initial balance.
    2. Store the initial balance from the user, then pass it to your constructor upon initializing the variable.

    I tried #2 and declaring the variable inside the switch statement. C++ doesn't seem to like that. Got a crosses initialization error. From what I read, seems you can't initialize a variable inside a switch statement. So you might have to redo the menu, using if statements.

    Or maybe someone more experienced has a better idea.
    "The best software is the one that fits your needs." - A_A

    Virus free since: date unknown
    Anti-virus free since: August 2009

  5. #5
    Join Date
    Mar 2006
    Posts
    325
    Rep Power
    0

    Default

    lovepythoin, thats not how you call a constructor and to get by the crosses initialization error you can put braces around the code in each case
    http://ideone.com/unKuu1

    you could use argc argv to set the initial value with the constructor before you run the program but then you wouldnt be able to ask the user for a value
    1337

  6. #6
    Join Date
    Sep 2005
    Posts
    2,394
    Rep Power
    0

    Default

    Yea i found that info and have been testing it. I got rid of the error but having a issue now. if do this for the default constructor
    Code:
    Account(){
            balance = 0;
        }
    It passes the value to
    Code:
    Account::Account(int a){
        
        if (a >= 0){
            
            balance = balance + a;
        }
        else {
            
            cout << "Invalid Amount Entered" << endl;
            balance = 0;
        }
    }
    but does not update the balance figure, remains at 0.

    If use this
    Code:
    Account(){
            //balance = 0;
        }
    It updates the balance to 47 no matter what positive figure enter for the opening balance. However if a place a negative amount it prints the error message. So i know that its receiving the value.

    edited case statement being used
    Code:
    case 1:{
                            
                            cout << "Please Enter Opening Balance amount";
                            cin >> amount;
                            Account user1(amount);}
                            break;
    To find what you seek in the road of life, the best proverb of all is that which says: "Leave no stone unturned." Edward Bulwer Lytton

  7. #7
    Join Date
    Mar 2006
    Posts
    325
    Rep Power
    0

    Default

    put the code up on ideone
    1337

  8. #8
    Join Date
    Sep 2005
    Posts
    2,394
    Rep Power
    0

    Default

    @wiel what you did was to put the output statement in the same class. I cant do that cause the assignment asks for the getBalance function

    Here's the entire question:
    Create a class called Account that a bank might use to represent customers' bank accounts. Your class should include one data member of type int to represent the account balance. Your class should provide a constructor that receives an initial balance and uses it to initialize the data member. The constructor should validate the initial balance to ensure that it is greater than or equal to 0. If not, the balance should be set to 0 and the constructor should display an error message, indicating that the initial balance was invalid. The class should provide three member functions. Member function credit should add an amount to the current balance. Member function debit should withdraw money from the Account and should ensure that the debit amount does not exceed the Account's balance. If it does, the balance should be left unchanged and the function should print a message indicating "Debit amount exceeded account balance." Member function getBalance should return the current balance. Create a program that creates two Account objects and tests the member functions of class Account.
    To find what you seek in the road of life, the best proverb of all is that which says: "Leave no stone unturned." Edward Bulwer Lytton

  9. #9
    Join Date
    Mar 2006
    Posts
    325
    Rep Power
    0

    Default

    Member function getBalance should return the current balance
    getBalance must be a member of a class. oh and dont use global functions in c++, bad practice
    1337

  10. #10
    Join Date
    Sep 2005
    Posts
    2,394
    Rep Power
    0

    Default

    in global functions are you referring to the menu functions that i created outside the class? getBalance is a member function of the class Account but when i create the declaration of user1 within the case and pass the variable that the user enters, the getBalance function that is outside the case does not pick up that value. Maybe i need to rethink the approach to the question
    Code:
    /* 
     * File:   main.cpp
     * Author: Dwayne Smith
     * ID: 20111053
     *
     * Created on October 17, 2012, 9:08 AM
     */
    
    #include <iostream>
    
    using namespace std;
    
    /*
     * 
     */
    
    int choice;
    int choice2;
    
    class Account {
        
    private:
        int balance;
        
    public:
        Account();/*{
            
            //balance = 0;
        }*/
            
        Account(int a);
        int creditAccount();
        int debitAccount();
        void getBalance();
    };
    
    void menu();
    void submenu();
    
    int main(int argc, char** argv) {
        
        int amount;
        //Account user1(0);
        //Account user1;
        //Account user2;
        
        do {
            
            menu();
    
            cout << "Choice: ";
            
            cin >> choice;
            
            switch (choice) {
                
                case 1:
                    
                    submenu();
                    cout << "Choice: ";
                    cin >> choice2;
                    
                    switch (choice2){
                        
                        case 1:{
                            
                            cout << "Please Enter Opening Balance amount";
                            cin >> amount;
                            Account user1(amount);
                            user1.getBalance();}
                            break; 
                            
                        case 2:{
                            
                            cout << "Please Enter Opening Balance amount";
                            cin >> amount;
                            Account user2(amount);}
                            break;
                            
                        case 9:
                            break;
                    }
                    break;
                    
                case 2:
                    
                    submenu();
                    cout << "Choice: ";
                    cin >> choice2;
                    
                    switch (choice2){
                        
                        case 1:
                            
                            //user1.getBalance();
                            break;
                            
                        case 2:
                            
                            //user2.getBalance();
                            break;
                            
                        case 9:
                            break;
                    }
                    break;
                    
                case 3:
                    
                    submenu();
                    cout << "Choice: ";
                    cin >> choice2;
                    
                    switch (choice2){
                        
                        case 1:
                            
                            cout << "Please Enter Lodgement amount";
                            cin >> amount;
                            //user1.creditAccount();
                            break;
                            
                        case 2:
                            
                            cout << "Please Enter Lodgement amount";
                            cin >> amount;
                            //user2.creditAccount();
                            break;
                            
                        case 9:
                            break;
                    }
                    break;
                    
                case 4:
                    
                    
                    
                case 9:
                    
                    cout << "Thanks for Processing! Have a good Day!";
                    break;
                    
                default:
                    
                    cout << "Invalid Option Selected. Please Try again!";
                    break;
                    cout << "Choice: ";
                    cin >> choice;
                
            }
                    
            
        }while (choice != 9);
    
        return 0;
    }
    
    void menu(){
        
        cout << "###############################################################" << endl << 
                "##                  Account Transaction Menu                 ##" << endl <<
                "##                                                           ##" << endl <<
                "##                                                           ##" << endl <<
                "##   Please select the number for the option needed:         ##" << endl <<
                "##                                                           ##" << endl <<
                "##   1. Enter Account Opening Balance                        ##" << endl <<
                "##   2. Check Account Balance                                ##" << endl <<
                "##   3. Make Account Lodgement                               ##" << endl <<
                "##   4. Make Withdrawal from Account                         ##" << endl <<
                "##   9. Exit                                                 ##" << endl <<
                "##                                                           ##" << endl <<
                "###############################################################" << endl;
    }
    
    void submenu(){
        
        cout << "###############################################################" << endl << 
                "##                  Account Transaction Menu                 ##" << endl <<
                "##                                                           ##" << endl <<
                "##                                                           ##" << endl <<
                "##   Please select the number for the option needed:         ##" << endl <<
                "##                                                           ##" << endl <<
                "##   1. User1                                                ##" << endl <<
                "##   2. User2                                                ##" << endl <<
                "##   9. Back to Main                                         ##" << endl <<
                "##                                                           ##" << endl <<
                "###############################################################" << endl;
    }
    
    Account::Account(int a){
        
        if (a >= 0){
            
            balance = a;
        }
        else {
            
            cout << "Invalid Amount Entered" << endl;
            balance = 0;
        }
    }
    
    int Account::creditAccount(){
        
        
    }
    
    int Account::debitAccount(){
        
        
    }
    
    void Account::getBalance(){
        
        cout << "The current available balance is: " << balance << endl << endl;
        cin.get();
    }
    Evertime i put my code in ideone it takes so darn long to give results
    To find what you seek in the road of life, the best proverb of all is that which says: "Leave no stone unturned." Edward Bulwer Lytton

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •