But it IS declared...isn't it?

Hello. I'm on my second day of c++ and I'm already stuck. Forgive me if I keep this brief - I just spent 10 minutes writing out my problem, then I hit the back button and lost it all!

The error I am getting is...

error: 'getBalance' undeclared (first use this function)

I wanted to use the getBalance() function in the debit fuction but got this error. But I noticed that the setBalance() function in the contructor didn't cause an error. So I moved the getBalance()function into the contructor and it didn't cause an error there. If I move the setBalance function to either the debit or credit function it DOES cause an error. I don't understand what's going on.

Can anyone help? I've got a feeling I'm missing an obvious type but I can't see it.

The header file

1
2
3
4
5
6
7
8
9
10
11
class Account {

    public:
        Account(int);
        void setBalance(int);
        int getBalance();
        void credit(int);
        void debit(int);
    private:
        int balance;
};


Here the getBalance() function is in the debit() function and causes the error.

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
#include <iostream>
#include "Account.h"

using namespace std;

Account::Account(int accountBalance) {

    if (accountBalance >= 0) {
        setBalance(accountBalance);

    } else {
         cout << "Initial balance invalid" << endl;

    }
}

void Account::setBalance(int accountBalance) {
    balance = accountBalance;

}

int Account::getBalance() {
    return balance;
}

void debit(int value) {
    cout << getBalance();
}

void credit(int value) {

}


Here the getBalance() function is in the constructor and doesn't cause the error.

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
#include <iostream>
#include "Account.h"

using namespace std;

Account::Account(int accountBalance) {
    cout << getBalance();

    if (accountBalance >= 0) {
        setBalance(accountBalance);

    } else {
         cout << "Initial balance invalid" << endl;

    }
}

void Account::setBalance(int accountBalance) {
    balance = accountBalance;

}

int Account::getBalance() {
    return balance;
}

void debit(int value) {

}

void credit(int value) {

}


closed account (1vRz3TCk)
you have forgot to add Account:: to the function definitions:
1
2
3
4
5
6
7
void Account::debit(int value) {
    cout << getBalance();
}

void Account::credit(int value) {

}
Last edited on
Oh my god! That is embarrassing. Thanks for your help. I couldn't see for looking, as my mum would say.
Last edited on
Topic archived. No new replies allowed.