Error" new_account was not declared in this scope

the following program is exactly as it is in the book by Walter Savitch but when I compile it I get the error in the title.

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
  //Program to demonstrate the class BankAccount.
#include<iostream>
using namespace std;

//Class for a bank account:
class BankAccount
{
public:
	void set(int dollars, int cents, double rate);
	//Postcondition: The account balance has been set to $dollars.cents;
	//The interest rate has been set to rate percent.
	
	void set(int dollars, double rate);
	//Postcondition: The account balance has been set to $dollars.00.
	//The interest rate has been set to rate percent.
	
	void update();
	//Postcondition: One year of simple interest has been
	//added to the account balance.
	
	double get_balance();
	//Returns the current account balance.
	
	double get_rate();
	//Returns the current account interest rate as a percentage.
	
	void output(ostream& outs);
	//Precondition: If outs is a file output stream, then
	//outs has already been connected to a file.
	//Postcondition: Account balance and interest rate have
	//been written to the stream outs.
	
	BankAccount new_account(BankAccount old_account);
	//Precondition: old_account has previously been given values
	//(that is, its member variables have been given values).
	//REturns the value for a new account that has a balance of zero
	//and the same interest rate as the old_account.
	
private:
	double balance;
	double interest_rate;
	
	double fraction(double percent);
	//Converts a percentage to a fraction. For example, fraction(50.3)
	//returns 0.503.
};

int main()
{
	BankAccount account1, account2;
	cout << "Start of Test:\n";
	account1.set(123, 99, 3.0);
	cout << "account1 initial statement:\n";
	account1.output(cout);
	account1.set(100, 5.0);
	cout << "account1 with new setup:\n";
	account1.output(cout);
	
	account1.update();
	cout << "account1 after update:\n";
	account1.output(cout);
	
	BankAccount account3, account4;
	account3.set(999, 99, 5.5);
	account4 = new_account(account3);
	account4.output(cout);
	
	return 0;
}

void BankAccount::set(int dollars, int cents, double rate)
{
	if ((dollars < 0) || (cents < 0) || (rate < 0))
	{
		cout << "Illegal values for money of interest rate.\n";
		exit(1);
	}
	
	balance = dollars + 0.01*cents;
	interest_rate = rate;
}

void BankAccount::set(int dollars, double rate)
{
	if ((dollars < 0) || (rate < 0))
	{
		cout << "Illegal values for money or interest rate.\n";
		exit(1);
	}
	
	balance = dollars;
	interest_rate = rate;
}

void BankAccount::update()
{
	balance = balance + fraction(interest_rate) * balance;
}

double BankAccount::fraction(double percent_value)
{
	return (percent_value/100.0);
}

double BankAccount::get_balance() 
{
	return balance;
}

double BankAccount::get_rate()
{
	return interest_rate;
}

//Uses iostream
void BankAccount::output(ostream& outs)
{
	outs.setf(ios::fixed);
	outs.setf(ios::showpoint);
	outs.precision(2);
	outs << "Account balance $" << balance << endl;
	outs << "Interest rate " << interest_rate << "%" << endl;
}

BankAccount new_account(BankAccount old_account)
{
	BankAccount temp;
	temp.set(0, old_account.get_rate() );
	return temp;
}

According to the author the program would produce the following output:
Account balance $0.00;
Interest rate 5.50%

I just can find where the problem is.
new_account is declared as a member of the class BankAccount(Line 33),
but defined as a free function(Line 125).
Last edited on
On the summary of Some properties of Classes the last one that I am quoting as it is written in the textbook.

A function may return an object; that is, a class may be the type for the value returned by a function. (See Self-Test Exercise 21.)

Now how do I fix an error that is an example by the author?
One way to fix it is to move lines 33-37 outside the class.

Another option is to change line 65 to account4 = account4.new_account(account3);
and change line 125 to BankAccount BankAccount::new_account(BankAccount old_account);
Thanks Thomas1965 the second option solved the problem.
Some of the problems we get here can be very difficult especially if it is wrong in the textbook.
Registered users can post here. Sign in or register to post.