Undefined variable in a class

Hello,
I'm doing the all-to-familiar bank program, and I having trouble with one of my variables. I'm trying the keep "balance" private, and just have it affected by the functions. The code is in 3 files: Account.h, Account.cpp, and Lab7.cpp

Thanks!

Account.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
  #include <iostream>
using namespace std;
class Account
{
private:
	double balance = 0;
	
public:
	Account();
	Account(double startingBal);
	double getBalance();
	void deposit(double amount);
	void withdraw(double amount);
	void addInterest(double interestRate);
};



Account.cpp
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
#include <iostream>
using namespace std;
#include "Account.h" //Account class Implementation
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);
	}


Lab7.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
#include<iostream>
using namespace std;
#include "Account.h"
int main()
{
	Account		a1;
	Account	a2(500);
	a1.deposit(200);
	a2.withdraw(50);
	a1.addInterest(0.2);
	cout << a1.getBalance();
	cout << "\n";
	cout << a2.getBalance();
	system("pause");
	return 0;
}
closed account (SECMoG1T)
Account cpp line 7 &8 possible identifier conflict
Thanks for the quick reply! I did change that, as well as some other things. I got rid of startingBal. Now it looks like my functions are unresolved. At least that is what the error messages say when I try to compile. I think the issue is that I added double balance into the parentheses, but if I don't, I get an undefined error for balance. Here is where I'm at now:


Account.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
#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);
};


Account.cpp
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
#include <iostream>
using namespace std;
#include "Account.h" //Account class Implementation
Account::Account()

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

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



lab7.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
#include<iostream>
using namespace std;
#include "Account.h"
int main()
{
	Account		a1;
	Account	a2(500);
	a1.deposit(200);
	a2.withdraw(50);
	a1.addInterest(0.2);
	cout << a1.getBalance();
	cout << "\n";
	cout << a2.getBalance();
	system("pause");
	return 0;
}
closed account (SECMoG1T)
A couple errors still

Account cpp 14 .... your function takes a param balance .... possible conflict

Deposit withdrawal addintrest in account.h takes single param each as opposed to two in Account cpp definition

avoid passing balance as an argument to any function that simply created more trouble for you
Last edited on
I noticed that it was causing errors because lab7.cpp wasn't giving balance any definitions in the parentheses for the functions. I removed it, but now I get errors for balance not being declared in each function in Account.cpp. Where should it be defined to pick up all of the functions?

This time, I'm just attaching Account.cpp, as that is where the trouble is ( I think).


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
#include <iostream>
using namespace std;
#include "Account.h" //Account class Implementation
Account::Account()

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

	void deposit(double amount)
	{
		balance += amount;
	}
	void withdraw(double amount)
	{
		balance -= amount;
	}
	void addInterest(double interestRate)
	{
		balance = balance*(1 + interestRate);
	}
closed account (SECMoG1T)
Maybe you can give a brief idea about what you want your class to do

Account.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
  #ifndef ACCOUNT_H
  #define ACCOUNT_H

  class Account 
    { 
       private: 	
              double balance ; 
              
       public: 	
             Account(); 	
             Account(double startingBal);/// you forgot to define this one
             double getBalance() const; 	
             void deposit(double amount); 
       	     void withdraw(double amount); 	
             void addInterest(double interestRate); 
    };
  #endif //ACCOUNT_H 


Account.cpp
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
 /* #include <iostream> using namespace std;*///can do without these

 #include "Account.h" //Account class Implementation 

 Account::Account() : balance (0) // this is enough
  { 	
    /*double balance = 0;*/ /// local variable ... conflicting with private member balance
  }
 
  Account::Account(double startingBal) :balance (startingBal) {}
  
  Account Account::getbalance() const /// this function should not change anything
  { 	
     return balance;
  } 	

  void Account::deposit(double amount) 	
  { 		
     balance += amount; 	
  } 	

  void Account::withdraw(double amount) 	
  { 		
     balance -= amount; 	
  } 	

  void Account::addInterest(double interestRate) 
  { 		
     balance = balance*(1 + interestRate); 	
  } 


Lab7.cpp

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
  
 #include <iostream>
 #include "Account.h" 

 using namespace std;

  int main() 
   { 	
      Account a1; 	
      Account a2(500); 	

     a1.deposit(200); 	
     a2.withdraw(50); 	
     a1.addInterest(0.2); 	

     cout << a1.getBalance(); 	
     cout << "\n"; 	
     cout << a2.getBalance(); 	

     system("pause"); 	
     return 0;
   }


I can't guess any better than that about your aim
Last edited on
Topic archived. No new replies allowed.