2 .h 1 implentation and main. private variables

My instructor provided us with the main and the 2 .h had us make a .cpp file.

The compiler keeps saying savings and checking was not declared in this scope when I try to compile it.

I have been searching for last 6 hours can somebody help please
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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
#ifndef ACCOUNT_H
#define	ACCOUNT_H

#include <iostream>
#include <string>
#include <iomanip>

using namespace std;

class Account {
public:
    Account();
    Account(double bal);
    ~Account(); // destructor
    void deposit(double amount);
    void withdraw(double amount);
    double get_balance() const;

private:
    double balance;
};

#endif	/* ACCOUNT_H */
//======================================

#ifndef BANK_H
#define	BANK_H

#include <string>
using namespace std;

#include "Account.h"

class Bank {
public:
    Bank();
    Bank(double checking_amount, double savings_amount);
    ~Bank(); // Destructor
    void deposit(double amount, string account);
    void withdraw(double amount, string account);
    void transfer(double amount, string account);
    void print_balances() const;

private:
    Account checking;
    Account savings;
};

#endif	/* BANK_H */

//===========================================
#include "Bank.h"

    //Bank::Bank();
Bank::Bank(double checking_amount, double savings_amount)
{
      checking=checking_amount;
      savings=savings_amount;
}

void deposit(double amount, string account)
{
    if(account=="S")
    {
        savings=savings+amount;
    }
    else if(account=="C")
    {
        checking=checking+amount;
    }
    else;
}
void withdraw(double amount, string account)
{
    if(account=="S")
    {
        savings=savings-amount;
    }
    else if(account=="C")
    {
        checking=checking-amount;
    }
    else;


}

void transfer(double amount, string account)
{
    if(account=="S")
    {
        checking=savings-amount;
        savings=checking+amount;

    }
    else if(account=="C")
    {
         savings=checking-amount;
        checking=savings+amount;

    }
    else;
}

void print_balances() const
{
cout<<"Savings account balance:  $"<<checking<<endl;
 cout<<"Checking account balance:  $"<<savings<<endl;
}
Bank::~Bank()
{

}
//==========================================
#include <iostream>
using namespace std;

#include "Bank.h"

int main() {

    Bank my_bank;
    cout << "Inital bank balances: " << endl;
    my_bank.print_balances(); /* set up empty accounts */

    cout << "Adding some money to accounts: " << endl;
    my_bank.deposit(1000, "S"); /* deposit $1000 to savings */
    my_bank.deposit(2000, "C"); /* deposit $2000 to checking */
    my_bank.print_balances();

    cout << "Taking out $1500 from checking,and moving $200 from";
    cout << " savings to checking." << endl;
    my_bank.withdraw(1500, "C"); /* withdraw $1500 from checking */
    my_bank.transfer(200, "S"); /* transfer $200 from savings */
    my_bank.print_balances();

    cout << "Trying to transfer $900 from Checking." << endl;
    my_bank.transfer(900, "C");
    my_bank.print_balances();

    cout << "trying to transfer $900 from Savings." << endl;
    my_bank.transfer(900, "S");
    my_bank.print_balances();

    return 0;
}
//=====================================
1
2
3
4
void deposit(double amount, string account)
{
    // ...
}


This is a free-standing function that is not a member of the class Bank

To define the method of Bank use:
1
2
3
4
void Bank::deposit(double amount, string account)
{
    // ...
}


thank you
okay so ya I did that now it seems repeat a error

"no match for 'operator+..." so I added a #include <cmath> but almost the samething happened

then it still happened stating that the operator types and Account and double....

wonder if it has something to do with

if(account=="S") //line 63
Last edited on
I just realized I was supposed to have 2 .cpp files that where not main. let me think about that for a sec

so I added this file

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
      #include "Account.h"
   #include <cmath>
   Account:: Account(double bal)
   {
        balance = bal;
   }
    void Account :: deposit(double amount)
    {
        balance = amount + balance;
    }
    void Account :: withdraw(double amount)
    {
        balance = balance - amount;
    }
    double Account :: get_balance() const
    {
       return balance;
    }
    Account::~Account() // destructor
    {

    }

and changed the second file to this

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
#include "Bank.h"
#include <cmath>
    //Bank::Bank();
Bank::Bank(double checking_amount, double savings_amount)
{
      checking=checking_amount;
      savings=savings_amount;
}

void Bank :: deposit(double amount, string account)
{
    if(account=="S")
    {
        savings.deposit(amount);
    }
    else if(account=="C")
    {
        checking.deposit(amount);
    }
    else;
}
void Bank :: withdraw(double amount, string account)
{
    if(account=="S")
    {
        savings.withdraw(amount);
    }
    else if(account=="C")
    {
        checking.withdraw(amount);
    }
    else;


}
//=======================================
void Bank :: transfer(double amount, string account)
{
      if(account=="S")
    {

        savings.withdraw(amount);
        checking.deposit(amount);
    }
    else if(account=="C")
    {
        checking.withdraw(amount);
        savings.withdraw(amount);
    }
    else;
}
//=======================================
void Bank :: print_balances() const
{
cout<<"Savings account balance:  $"<<savings.get_balance()<<endl;
 cout<<"Checking account balance:  $"<<checking.get_balance()<<endl;
}
Bank::~Bank()
{

}


at this point it went to this error:

||=== Build: Debug in p1 (compiler: GNU GCC Compiler) ===|
obj/Debug/Bank.o||In function `Bank::Bank(double, double)':|
/home/jydilaptop/Documents/p1/Bank.cpp|3|undefined reference to `Account::Account()'|
/home/jydilaptop/Documents/p1/Bank.cpp|3|undefined reference to `Account::Account()'|

obj/Debug/main.o||In function `main':|
/home/jydilaptop/Documents/p1/main.cpp|8|undefined reference to `Bank::Bank()'|
||=== Build failed: 3 error(s), 0 warning(s) (0 minute(s), 0 second(s)) ===|
Last edited on
1
2
3
4
5
6
7
8
9
10
11
12
13
void Bank :: transfer(double amount, string account)
{
    if(account=="S")
    {
        savings.withdraw(amount);
        checking.deposit(amount);
    }
    else if(account=="C")
    {
        checking.withdraw(amount);
        savings.deposit(amount);
    }
}


thank you, I realized right about the same time as the post :D (the above post was edited before I realized you had posted, sorry)

but now I get this error

but I still the the above error from the edited post

Topic archived. No new replies allowed.