Desperate for assistance with this.

I can't figure out what I'm doing wrong. I keep getting an error that "no matching function for call to ‘BankAccount::BankAccount()"

I'm not sure I did the constructor right, my homework says "The data members of this class do not need to be set after they are initialized, so this class doesn't need any set methods - therefore the constructor will directly assign values to the data members instead of calling set methods to do it. " Did I do that right or no?

If anyone can point me in the right direction I would really appreciate it.

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

#ifndef BankAccount_hpp
#define BankAccount_hpp
#include <string>

class BankAccount

{
private:
    std::string customerName;
    std::string customerID;
    double customerBalance;
    
public:
    BankAccount (std::string, std::string, double);
    std::string getCustomerName ();
    std::string getCustomerID ();
    double getCustomerBalance ();
    double withdraw (double);
    double deposit (double);
    
};

#endif




#include "BankAccount.hpp"
#include <iostream>
#include <string>
using namespace std;

BankAccount::BankAccount (string name, string ID, double balance)
{
    customerName = name;
    customerID = ID;
    customerBalance = balance;
}

string BankAccount::getCustomerName()
    {
        return customerName;
    }

string BankAccount::getCustomerID()
    {
        return customerID;
    }

double BankAccount::getCustomerBalance()
    {
        return customerBalance;
    }

double BankAccount::withdraw(double withdraw)
    {
        return customerBalance - withdraw;
    }

double BankAccount::deposit(double deposit)
    {
        return customerBalance - deposit;
    }
Last edited on
BankAccount (std::string, std::string, double);
you are sayring that to construct an `BankAccount' object you need to provide three things, by instance,
BankAccount foo("N N", "123421", 1e12);

But that was not how you were constructing your object (code that for some obscure reason you've decided not to show)
Last edited on
I don't understand. If you mean the main function, I'm not being graded on that. But here is is. I don't at all understand what you by that's not how the object was constructed.

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
int main ()
{
    string CustomerName;
    string CustomerID;
    int choice;
    double Balance;
    double theWithdraw;
    double theDeposit;
    
    
    BankAccount Account1;
    Balance=200
    CustomerName=Jocob
    CustomerID=42
    
    cout << "press 1 for withdraw and 2 for deposit." <<endl;
    
    cin >> choice;
    
    if (choice == 1)
    {
        cout << "How much would you like to withdraw?" << endl;
        cin >> theWithdraw;
        
        cout << "Your new balance is " << Account1.withdraw(theWithdraw) << endl;
    }
    
    if (choice ==2)
    {
        cout << "How much would you like to deposit?" << endl;
        cin >> theDeposit;
        
        cout << "Your new balance is " << Account1.deposit(theDeposit) << endl;
    }
    
    return 0;
    
}
Last edited on
As ne555 said, your constructor requires information. You are not supplying that information to the constructor. The compiler is complaining because you're not doing what you told it is required for creating a BankAccount.

1
2
3
4
5
6
7
8
9
    // ...
    string CustomerID = 42;
    string CustomerName = "Jocob";
    double Balance = 200.00;

    // supply the requisite information:
    BankAccount Account1(CustomerName, CustomerID, Balance);

    // ... 

Thank You!! I have spent three days trying to fix it. Oh my god, thank you. I kept thinking I messed the header file up.
closed account (48T7M4Gy)
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
#include <string>
#include <iostream>

using namespace std;

class BankAccount {
private:
	std::string customerName;
	std::string customerID;
	double customerBalance;

public:
	BankAccount(); // <-- default constructor
	BankAccount(std::string, std::string, double);
	std::string getCustomerName();
	std::string getCustomerID();
	double getCustomerBalance();
	double withdraw(double);
	double deposit(double);

};

BankAccount::BankAccount() // default constructor
{
	customerName = "No name given";
	customerID = "Unknown";
	customerBalance = 0;
}

BankAccount::BankAccount(string name, string ID, double balance)
{
	customerName = name;
	customerID = ID;
	customerBalance = balance;
}

string BankAccount::getCustomerName()
{
	return customerName;
}

string BankAccount::getCustomerID()
{
	return customerID;
}

double BankAccount::getCustomerBalance()
{
	return customerBalance;
}

double BankAccount::withdraw(double withdraw)
{
	return customerBalance - withdraw;
}

double BankAccount::deposit(double deposit)
{
	return customerBalance - deposit;
}

int main()
{
	string CustomerName;
	string CustomerID;
	int choice;
	double Balance;
	double theWithdraw;
	double theDeposit;

	BankAccount Account1;
	Balance = 200; // <---
	CustomerName = "Jocob"; // <---
	CustomerID = 42; // <---

	cout << "press 1 for withdraw and 2 for deposit." << endl;

	cin >> choice;

	if (choice == 1)
	{
		cout << "How much would you like to withdraw?" << endl;
		cin >> theWithdraw;

		cout << "Your new balance is " << Account1.withdraw(theWithdraw) << endl;
	}

	if (choice == 2)
	{
		cout << "How much would you like to deposit?" << endl;
		cin >> theDeposit;

		cout << "Your new balance is " << Account1.deposit(theDeposit) << endl;
	}

	return 0;

}


As with the comments above if you look carefully at the changes here you'll see what a default constructor, in this case where you don't supply details, might look like and how it operates with your main. Unless you use the second constructor or you write some 'setters', creating a BankAccount object using the default constructor means Jocob will never exist as far as that object is concerned.
Topic archived. No new replies allowed.