Error "use of undeclared identifier"

Hello,
I am having trouble with my main .cpp file, here is the code so far:

#include <iostream>

using namespace std;
#include "Account.h"
Account::Account()

{
double balance=0;
balance=0;
}
Account getbalance()
{
return balance;
}

void deposit(double amount)
{
balance+=amount;
}
void withdraw(double amount)
{
balance-=amount;
}
void addInterest(double interestRate)
{
balance=balance*(1+interestRate);
}

Any ideas? Thanks

**Here is my header file Account.h-
#include <iostream>
using namespace std;
class Account
{
private:
double balance;
public:
Account();
Account(double);
double getBalance();
void deposit(double amount);

void withdraw(double amount);
void addInterest(double interestRate);
};
Last edited on
You need to provide more information regarding the issue, especially your header file.

Anyways, it seems like the error is pointing to the variable balance.
You declare a variable 'double balance' in your constructor.
When you declare a variable in a function in, it's life ends when the function ends and hence you can't use it in other functions. And probably that's why it is showing the error "undeclared identifier"

In this case, you should declare "double balance" in your header file so that any other function can use that variable.

Hope this helps!
Last edited on
Thank you! I just edited my question to include the header file.
Please use code tags!

"How to use code tags"
http://www.cplusplus.com/articles/jEywvCM9/

Then...

I can see that you haven't implemented your class member functions Account::deposit(), Account::withdraw(), Account::addInterest() -- instead you've implemented free functions with the same names.

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

using namespace std;
#include "Account.h"

Account::Account()
{
    double balance=0; // <= should not be here as hides member variable
    balance=0;
}

// <= missing constructor implementation?

Account getbalance() // <= this line is broken, too
{
    return balance;
}

void deposit(double amount) // <= not a member function
{
    balance+=amount;
}

void withdraw(double amount) // <= not a member function
{
    balance-=amount;
}

void addInterest(double interestRate) // <= not a member function
{
     balance=balance*(1+interestRate);
}


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
#include <iostream>
using namespace std; // <= should not use 'using' in a header file!
class Account
{
private:
    double balance;
public:
    Account();
    Account(double);
    double getBalance();
    void deposit(double amount);

    void withdraw(double amount);
    void addInterest(double interestRate);
}; 
Last edited on
Topic archived. No new replies allowed.