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

1. ## 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();
};

int main(int argc, char** argv) {

int amount;
Account user1;
Account user2;

do {

cout << "Choice: ";

cin >> choice;

switch (choice) {

case 1:

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:

cout << "Choice: ";
cin >> choice2;

switch (choice2){

case 1:

user1.getBalance();
break;

case 2:

user2.getBalance();
break;

case 9:
break;
}
break;

case 3:

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;
}

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;
}

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();
}``` Reply With Quote

2. ## 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);` Reply With Quote

3. ## 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? Reply With Quote

4. ## Originally Posted by lovepython 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. Reply With Quote

5. ## 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 Reply With Quote

6. ## 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;``` Reply With Quote

7. ## put the code up on ideone Reply With Quote

8. ## @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. Reply With Quote

9. ## 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 Reply With Quote

10. ## 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();
};

int main(int argc, char** argv) {

int amount;
//Account user1(0);
//Account user1;
//Account user2;

do {

cout << "Choice: ";

cin >> choice;

switch (choice) {

case 1:

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:

cout << "Choice: ";
cin >> choice2;

switch (choice2){

case 1:

//user1.getBalance();
break;

case 2:

//user2.getBalance();
break;

case 9:
break;
}
break;

case 3:

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;
}

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;
}

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 Reply With Quote

#### Posting Permissions

• You may not post new threads
• You may not post replies
• You may not post attachments
• You may not edit your posts
•
{"ft_lang_label":"__label__en","ft_lang_prob":0.8462776,"math_prob":0.697392,"size":761,"snap":"2020-24-2020-29","text_gpt3_token_len":189,"char_repetition_ratio":0.15191546,"word_repetition_ratio":0.0,"special_character_ratio":0.28383705,"punctuation_ratio":0.1513158,"nsfw_num_words":0,"has_unicode_error":false,"math_prob_llama3":0.95449305,"pos_list":[0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27,28,29,30,31,32,33,34,35,36,37,38,39,40,41,42,43,44,45,46,47,48,49,50,51,52,53,54,55,56,57,58,59,60,61,62,63,64,65,66,67,68],"im_url_duplicate_count":[null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null],"WARC_HEADER":"{\"WARC-Type\":\"response\",\"WARC-Date\":\"2020-05-25T02:05:47Z\",\"WARC-Record-ID\":\"<urn:uuid:485bddbc-a067-4645-9684-3f50a9440ba2>\",\"Content-Length\":\"104244\",\"Content-Type\":\"application/http; msgtype=response\",\"WARC-Warcinfo-ID\":\"<urn:uuid:3fca702f-2db9-4bb9-b5be-cca55e09f57c>\",\"WARC-Concurrent-To\":\"<urn:uuid:73de9058-6e02-4498-a7c3-901bdf6082d9>\",\"WARC-IP-Address\":\"198.54.115.22\",\"WARC-Target-URI\":\"http://www.techjamaica.com/forums/showthread.php?109347-Why-am-i-getting-a-invalid-use-of-Class&s=9d683f144555866b12ec9aced46f9339\",\"WARC-Payload-Digest\":\"sha1:PLWMW4VDG5CK3XEET7KYBFYWBODSSEPH\",\"WARC-Block-Digest\":\"sha1:EP72DUPXVY46NRUVRGAHEMZPQ55I4VT4\",\"WARC-Identified-Payload-Type\":\"application/xhtml+xml\",\"warc_filename\":\"/cc_download/warc_2020/CC-MAIN-2020-24/CC-MAIN-2020-24_segments_1590347387155.10_warc_CC-MAIN-20200525001747-20200525031747-00326.warc.gz\"}"}