Error when building my class

Hi, i am busy with a project from school and i am creating a class of a bank account. When i build the class i get an error on 1 of the functions and i dont understand why?

Error below:

error: non-member function 'double getAccountBalance()' cannot have cv-qualifier
error: 'accountBalance' was not declared in this scope

Below my header file:

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
#ifndef BANKACCOUNT_H
#define BANKACCOUNT_H

#include <iostream>
#include <iomanip>

using namespace std;

class bankAccount
{
    public:

        void setAccountNumber(int number);
            //Function to set account number.
            //The member variables accountNumber
            //is set according to the parameters.
            //Postcondition: accountNumber = number;

        int getAccountNumber() const;
            //Function to return the account number.
            //Postcondition: The value of accountNumber is returned.

        void setAccountBalance(double balance);
            //Function to set account balance.
            //The member variables accountBalance
            //is set according to the parameters.
            //Postcondition: accountBalance = balance;

        double getAccountBalance();
            //Function to return the account balance.
            //Postcondition: The value of accountBalance is returned.

        void acceptDeposit(double deposit);
            //Function to receive the amount depositied by
            //the customer and update the amount in the account.
            //Postcondition: accountBalance = accountBalance + deposit;

        void acceptWithdrawal(double withdrawal);
            //Function to withdraw money from the account
            //and to update the amount in the account.
            //Postcondition: accountBalance = accountBalance - withdrawal;

        void printAccount() const;
            //Function to output the account details in the form
            //Acc No. XXXXXXXXX
            //Acc Ba. R0.00

        bankAccount(int number = 0, double balance = 0);
            //Constructor to set the bank account number and balance.
            //The member variables accountNumber and accountBalance
            //are setaccording to the parameters.
            //Postcondition: accountNumber = number, accountBalance = balance
            //               If no values are specified, the default
            //               values are used to initialize the member
            //               variables.

    private:
        int accountNumber;//variable to store account number.
        double accountBalance;//variable to store account balance.
};

#endif // BANKACCOUNT_H 


Below my implementation file:

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
#include <iostream>
#include <iomanip>

#include "bankAccount.h"

using namespace std;

        void bankAccount::setAccountNumber(int number)
        {
            accountNumber = number;
        }

        int bankAccount::getAccountNumber() const
        {
            return accountNumber;
        }

        void bankAccount::setAccountBalance(double balance)
        {
            accountBalance = balance;
        }

        double getAccountBalance() const
        {
            return accountBalance;
        }

        void bankAccount::acceptDeposit(double deposit)
        {
            accountBalance = accountBalance + deposit;
        }

        void bankAccount::acceptWithdrawal(double withdrawal)
        {
            accountBalance = accountBalance - withdrawal;
        }

        void bankAccount::printAccount() const
        {

        }

        bankAccount::bankAccount(int number, double balance)
        {
            accountNumber = number;
            accountBalance = balance;
        }
Notice that you forgot to prepend the method name with bankAccount:: in the .cpp file.

Looking good so far!
Your member function signatures must match exactly in the declarations and definitions.

In your header you need to qualify the getAccountBalance member function as const, or remove it from your class implementation of getAccountBalance.

In your class implementation file it is bankAccount::getAccountBalance.

Sometimes it is the little things that trip us up, especially after staring at a bit of code for a while.
Thank you very much! Its always the little things that catch me...
Thank you for showing us the errors, too often people don't make the effort.

The compile-time errors may seem at first a bit daunting and hard to understand, but as some experience is gained they become easier to decipher. The two errors you showed are 'typical' class "boo-boos."
This code reminds me of a massive old book from Jesse Liberty with which I learnt C++
Always the same codes and context in order to learn Classes :/
Last edited on
Jesse Liberty has written a number of C++ books, all the way back before there was an ISO standard for C++. I have his Sams book, "Teach Yourself ANSI C++ in 21 Days, Premier Edition" in my programming library, copyright © 1996.

Prominent cover blurb: "Covers the ANSI C++ Draft Standard" *SMH*

Yeah, before C++98. Here's the book's Chapter 1 "Hello World" example:
1
2
3
4
5
6
#include <iostream.h>

void main()
{
   cout << "Hello World!\n";
}

I kid you not, void frikkin' main?!?
I saw a book from J. Liberty saying that someone could learn C++ in 21 days...
I said to myself that it is just a title in order to catch newbies - a book promissing something impossible.
What do you think about these books? It worth the money?
Are they worth the money? Yes, and no. The books do teach a lot of what the C++ standard was at the time of writing in 21 days, or in one hour a day as more recent edition titles proclaim they can. They certainly don't teach all of what C++ has to offer. No one book can do that.

cppreference barely does that, by not being beginner friendly. The Learn C++ website doesn't show all of what C++ has to offer.

There are better books available for learning C++, though not as "easy" as the Sams boooks are.

One I like a lot is: "Beginning C ++ 20 : From Novice to Professional"
https://www.amazon.com/gp/product/1484258835/
Last edited on
Thanks for the Amazon link. Thumb up ++
If'n you do get the book consider downloading the examples and exercises source code from the github repo.
https://github.com/Apress/beginning-cpp20

I did it, as a git clone. The instructions are shown on the repo main page.

There have been several updates/changes since I first snagged the repo about 8 months ago. Including some stealth C++23 updates.

A change to one of the example source files was done 2 days ago.
Last edited on
@MHS1986 - it's not good practice to have a using namespace std in a header file. This can cause problems.

See:
https://github.com/isocpp/CppCoreGuidelines/blob/master/CppCoreGuidelines.md#Rs-using-directive

Also, the class constructor would usually be coded as:

1
2
bankAccount::bankAccount(int number, double balance)
        : accountNumber {number}, accountBalance {balance} {}

it's not good practice to have ...

unnecessary things.

Q: What in the header file requires:
1
2
3
4
#include <iostream>
#include <iomanip>

using namespace std;

A: Nothing

Q: What in the implementation file requires those?
A: Nothing

All you get with unnecessary includes is that the compiler has to do a bit more work. Granted, you won't notice it in toy problems, but it is hard to get rid of (bad) habits later.

Ask yourself: "Why did I add these lines?"

If'n you do get the book consider downloading the examples and exercises source code from the github repo.
https://github.com/Apress/beginning-cpp20

I did it, as a git clone. The instructions are shown on the repo main page.


Thank you GeorgeP for the GitHub repo. Downloaded ++
Hey guys, i am struggling with an inherited class i am creating but for some reason the wheels have fallen off and i cant figure out why.

below base class header file:
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
#ifndef BANKACCOUNT_H
#define BANKACCOUNT_H

class bankAccount
{
    public:

        void setAccountNumber(int number);
            //Function to set account number.
            //The member variables accountNumber
            //is set according to the parameters.
            //Postcondition: accountNumber = number;

        int getAccountNumber() const;
            //Function to return the account number.
            //Postcondition: The value of accountNumber is returned.

        void setAccountBalance(double balance);
            //Function to set account balance.
            //The member variables accountBalance
            //is set according to the parameters.
            //Postcondition: accountBalance = balance;

        double getAccountBalance() const;
            //Function to return the account balance.
            //Postcondition: The value of accountBalance is returned.

        void acceptDeposit(double deposit);
            //Function to receive the amount depositied by
            //the customer and update the amount in the account.
            //Postcondition: accountBalance = accountBalance + deposit;

        void acceptWithdrawal(double withdrawal);
            //Function to withdraw money from the account
            //and to update the amount in the account.
            //Postcondition: accountBalance = accountBalance - withdrawal;

        void printAccount() const;
            //Function to output the account details in the form
            //Account Number: XXXXXXXXX
            //Account Balance: R0.00

        bankAccount(int number = 0, double balance = 0);
            //Constructor to set the bank account number and balance.
            //The member variables accountNumber and accountBalance
            //are setaccording to the parameters.
            //Postcondition: accountNumber = number, accountBalance = balance
            //               If no values are specified, the default
            //               values are used to initialize the member
            //               variables.

    protected:
        int accountNumber;//variable to store account number.
        double accountBalance;//variable to store account balance.
};

#endif // BANKACCOUNT_H 


base class implementation file below:

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
#include <iostream>
#include <iomanip>

#include "bankAccount.h"

using namespace std;

void bankAccount::setAccountNumber(int number)
{
    accountNumber = number;
}

int bankAccount::getAccountNumber() const
{
    return accountNumber;
}

void bankAccount::setAccountBalance(double balance)
{
    accountBalance = balance;
}

double bankAccount::getAccountBalance() const
{
    return accountBalance;
}

void bankAccount::acceptDeposit(double deposit)
{
    accountBalance = accountBalance + deposit;
}

void bankAccount::acceptWithdrawal(double withdrawal)
{
    accountBalance = accountBalance - withdrawal;
}

void bankAccount::printAccount() const
{
    cout << fixed << showpoint << setprecision(2);
    cout << "Account Number: " << accountNumber << endl;
    cout << "Account Balance: R" << accountBalance << endl;
}

bankAccount::bankAccount(int number, double balance)
{
    accountNumber = number;
    accountBalance = balance;
}


below code for header file of problem class

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
#ifndef SAVINGSACCOUNT_H
#define SAVINGSACCOUNT_H

#include "bankAccount.h"

class savingsAccount : public bankAccount
{
    public:
        void setInterestRate(double interest);
            //Function to set account interest rate.
            //The member variable interestRate
            //is set according to the parameters.
            //Postcondition: interestRate = interest;

        double getInterestRate() const;
            //Function to return the account interest rate.
            //Postcondition: The value of interestRate is returned.

        void acceptWithdrawal(double withdrawal);
            //Function that first checks to see if there is enough funds
            //in the account. If there are enough funds the amount is withdrawn
            //from the account and the remaining funds updated.
            //If there is not enough funds a message is returned.
            //Postcondition: if (accountBalance >= withdrawel)
            //                  accountBalance = accountBalance - withdrawal;
            //               else
            //                  insuficient funds;

        void setInterest();
            //Function to set account interest earned.
            //The member variable interestEarned
            //is set according to the parameters.
            //Postcondition: interestEarned = interest;

        double getInterest() const;
            //Function to return the account interest earned.
            //Postcondition: The value of interestEarned is returned.

        void printCheck() const;
            //Function to output the account details in the form
            //**************************
            //Savings Account
            //**************************
            //Account Number: XXXXXXXXX
            //Account Balance: R0.00
            //**************************

        savingsAccount(int num = 0, double bal = 0, double intRate = 3, double intEarned = 0);
            //Constructor to set the bank account number, balance, interest rate and interest earned.
            //The member variables accountNumber, accountBalance, interestRate
            //and interestEarned are set according to the parameters.
            //Postcondition: accountNumber = num, accountBalance = bal
            //               interestRate = intRate, interestEarned = intEarned.
            //               If no values are specified, the default
            //               values are used to initialize the membervariables.

    private:
        double interestRate;//variable to store interest rate.
        double interestEarned;//variable to store interest earned.

#endif // SAVINGSACCOUNT_H 


and then code for implementation file that is giving a lot of errors i dont understand and cant figure out...

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
#include <iostream>
#include <iomanip>

#include "savingsAccount.h"

using namespace std;

void savingsAccount::setInterestRate(double interest)
{
    interestRate = interest;
}

double savingsAccount::getInterestRateSaving() const
{
    return interestRate;
}

void savingsAccount::acceptWithdrawalSaving(double withdraw)
{
    if (accountBalance >= withdraw)
            accountBalance = accountBalance - withdraw;
    else
        cout << "Insuficient funds!" << endl;
}

void savingsAccount::setInterestSaving()
{
    interestEarned = accountBalance * (interestRate/100);
}

double savingsAccount::getInterestSaving() const
{
    return interestEarned;
}

void savingsAccount::printCheckSaving() const
{
cout << "**************************";
cout << "Savings Account";
cout << "*************************";
bankAccount::printAccount();
cout << "*************************";
cout << endl;
}

savingsAccount::savingsAccount(int num, double bal, double intRate, double intEarned)
{
    bankAccount(num, bal);
    interestRate = intRate;
    interestEarned = intEarned;
}


Below errors experienced:

error: expected nested-name-specifier before 'namespace'
error: extra qualification 'savingsAccount::' on member 'setInterestRate
error: 'void savingsAccount::setInterestRate(double)' cannot be overloaded
error: with 'void savingsAccount::setInterestRate(double)'
error: extra qualification 'savingsAccount::' on member 'getInterestRateSaving'

The list goes on...
Would be helpful if you also posted the file/line number associated with each error message.

Usually, start with the first error, and sometimes the rest resolve themselves.

If what you copied is your real code, then you are missing a closing brace + semi-colon at the end of your savingsAccount class.
Shoh, once again a simple syntactical error... That fixed everything.
I was in a bit of a panic because my deadline was looming...
Gents thank you for all the help and input!

Assignment submitted. Now we wait :)
Topic archived. No new replies allowed.