C++ Classes

Not compiling correctly. Keeps throwing me LNK2019 errors.
Thanks for the help :)

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
//Account.h
#ifndef ACCOUNT_H
#define ACCOUNT_H
#include <iostream>
#include <cctype>
#include <iomanip>
using namespace std;

class Account {
private:
	double balance;
public:
	Account();
	~Account();
	void makeDeposit(double amount);
	void withdraw(double amount);
	double getBalance();
};
#endif

//Account.cpp
#include "Account.h"

Account::Account()
{
	this->balance = 0.0;
}

Account::~Account()
{

}

void Account::makeDeposit(double amount)
{
	this->balance += amount;
}

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

double Account::getBalance()
{
	return this->balance;
}

//main.cpp
#include "Account.h"

void displayMenu();
void makeDeposit(Account &);
void withdraw(Account &);

int main()
{
	Account savings;
	char choice;

	cout << fixed << showpoint << setprecision(2);

	do
	{
		displayMenu();
		cin >> choice;

		switch (toupper(choice))
		{
		case 'A': cout << "The current balance is $";
			break;
		case 'B': makeDeposit(savings);
			break;
		case 'C': withdraw(savings);
			break;
		case 'D': exit(1);
			break;
		default: cout << "Invalid selection!\n";
		}
	} while(toupper(choice) != 'D');

	return 0;
}

void displayMenu()
{
	cout << '\t' << "MENU" << '\t' << endl
		<< "-------------------------"
		<< "A) Display the account balance" << endl
		<< "B) Make a deposit" << endl
		<< "C) Make a withdraw" << endl
		<< "D) Exit the program" << endl;

	cout << "Enter your choice: ";
}
where is the implementation of void makeDeposite(Account &) and void withdraw(Account &)??
from what I understand, both prototypes connect to my classes functions. Please correct me if I am wrong.
To access a object's function you must use a '.' (or a -> for a pointer to an object). ex:

1
2
Account savings;
savings.makeDeposit(savings);


you're defining a function in the main file that has the same name as a function from Account class. This is ok, but you must give the function a body. Please note this isn't needed to access the object's functions.

anyone please correct me if I'm wrong.

if you're trying to use the object's functions to deposit, do this:

1
2
3
4
5
6
7
8
9
10
11
Account account;
double newDeposit;

if(input == 'C')
{

cin >> newDeposit;

account.makeDeposit(newDeposit);
}
Last edited on
kong288 is right.

lines 53 and 54 shouldn't be there. These function declarations mask the real errors. (They lead the compiler to expect global functions, which is what the linker will be complaining about.)

53
54
void makeDeposit(Account &);
void withdraw(Account &);


Which is that, on line 72 and t4, you should be calling you class' methods through an instance with the . (dot) operator. As your instance is called savings, e.g.

savings.makeDeposit(newDeposit);

Andy
Topic archived. No new replies allowed.