Pointers to Class and DMA

I am pretty new to this, and I am completely lost as to what is happening. I am working on a project that require us to use Dynamic Memory Allocation and pointers to a class. This is what I've got so far, but I'm not sure what I am doing wrong. I know that the program is not complete, but I feel like I need to get past this one problem before I can finish.

I'm getting this error. Line 94: error: no matching function for call to 'CheckingAccount::CheckingAccount();

Please help me!

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
#include <iostream>

using namespace std;

class Person
{
  public:

    string firstname;
    string lastname;
    int age;
    char gender;

    string get_firstname();
    string get_lastname();
    int get_age();
    char get_gender();
    void set_firstname(string);
    void set_lastname(string);
    void set_age(int);
    void set_gender(char);
};

class CheckingAccount
{

  Person p;

  int account_num;
  double balance;


  public:

    void display_account_information();
    CheckingAccount(int acc_num, double balance, string fn, string ln, int age, char gender);


};

CheckingAccount::CheckingAccount(int acc_num, double balance, string fn, string ln, int age, char gender)
{
    account_num = acc_num;
    balance = balance;
    p.firstname = fn;
    p.lastname = ln;
    p.age = age;
    p.gender = gender;
}

string Person::get_firstname()
{

}

string Person::get_lastname()
{

}

int Person::get_age()
{

}

char Person::get_gender()
{

}

void Person::set_firstname(string)
{
    cout << "Enter first name: ";
}

void Person::set_lastname(string)
{
    cout << "Enter last name: ";
}

void Person::set_age(int)
{
    cout << "Enter age: ";
}

void Person::set_gender(char)
{
    cout << "Enter gender (m/f): ";
}

int main()
{
    CheckingAccount *p_ca;
    p_ca= new CheckingAccount;


    string firstn;

    p_ca->Person::get_firstname;
    cin >> Person::set_firstname(string firstn);
}
On that line you are trying to create a new CheckingAccount calling the constructor with no parameters. You have no such constructor defined.
So would I need to change

p_ca= new CheckingAccount;

to something like

p_ca= CheckingAccount(int acc_num, double balance, string fn, string ln, int age, char gender)
What should I change to correct this problem? I am confused as to what you mean by I am trying to create a new CheckingAccount calling the constructor with no parameters.
bump
You should change that line in the way you describe, except that instead of "int acc_num" etc, you'd need to put variables (or values).
Thanks Gaminic, I've done that and it seems to be working but now I've got other problems.

I'm trying to display the first name string (just for now) using the

1
2
3
4
void CheckingAccount::display_account_information()
{
    cout << "First name is: "
}


Here is my newly re-written code. I'm thinking that the assignment wants me to get the information from the section in lines 34-41.

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
#include <iostream>

using namespace std;

class Person
{
    public:

    string firstname;
    string lastname;
    int age;
    char gender;

    string get_firstname();
    string get_lastname();
    int get_age();
    char get_gender();
    void set_firstname(string);
    void set_lastname(string);
    void set_age(int);
    void set_gender(char);
};

class CheckingAccount
{
    public:

    Person p;
    int account_num;
    double balance;

    void display_account_information();

    CheckingAccount(int acc_num, double balance, string fn, string ln, int age, char gender)
    {
        account_num = acc_num;
        balance = balance;
        p.firstname = fn;
        p.lastname = ln;
        p.age = age;
        p.gender = gender;
    };
};

void Person::set_firstname(string fname)
{
    fname = firstname;
}

void CheckingAccount::display_account_information()
{
    cout << "First name is: "
}

int main()
{
    CheckingAccount *p_ca;
    p_ca= new CheckingAccount(1, 1.00, "Chris", "Brem", 25, 'm');

    string first_name;

    cout << "First Name: ";
    cin >> first_name;
    cout << endl;

    p_ca->p.set_firstname(first_name);
}



What exactly is your problem/question?

You have the data and you have the function required to write. All you need to do is tell your function to output the variables holding the data, which can be done like any other output:
1
2
int a = 5;
cout << "The value of a: " << a << ".\n";
I guess my question if I were to use

 
cout << "First name is: " << p.firstname;


and in main ()

 
p_ca->display_account_information();


Why do I need to put this?

1
2
3
4
5
6
7
8
9
10
    CheckingAccount(int acc_num, double balance, string fn, string ln, int age, char gender)
    {
        account_num = acc_num;
        balance = balance;
        p.firstname = fn;
        p.lastname = ln;
        p.age = age;
        p.gender = gender;
    };
};
Yes and yes.

To your last question: Do you mean "what does this code do?"? If so: that's a constructor. It allows you to initialize an object of your class and assign starting values to its members.
It just seems like to much work to me, but maybe later I will understand.

I'm not sure I am understanding this guy correctly but he wants us to create 2 accounts and print them on the screen. How would I go about doing that?
Too much work? Without the constructor, you have to assign all values manually using set*() functions (which is more work, and also stupid).

You already have all the code required to "make two accounts". If you simply fill in the rest of your display_account_information() function, you'll be done.
See, already understand why I would want to do that instead now. Thanks.

I think I have 1 last question. Once I get all my display account information filled in. How could I display both accounts?

I would imagine he wants us to have it look this way. After putting in both people.


Account 1:

1) Account number: <checking account number> (e.g. 16523241)
2) Account balance: <current balance> (e.g. $1038.65) 
3) Account holder: <first name> <last name> (e.g. John Doe)
4) Account holder’s age: <age> (e.g. 24)
5) Account holder’s gender: <gender> (e.g. male)

Account 2:

1) Account number: <checking account number> (e.g. 16523241)
2) Account balance: <current balance> (e.g. $1038.65) 
3) Account holder: <first name> <last name> (e.g. John Doe)
4) Account holder’s age: <age> (e.g. 24)
5) Account holder’s gender: <gender> (e.g. male)

You can "hard-code" it:
1
2
3
4
5
6
7
CheckingAccount *p_ca;
p_ca= new CheckingAccount(1, 1.00, "Chris", "Brem", 25, 'm');
p_ca->display_account_information();

CheckingAccount *p_cb;
p_cb= new CheckingAccount(2, 2.00, "Sirhc", "Merb", 52, 'f');
p_cb->display_account_information();
Gaminic, you don't realize what you've done. I am having a VERY hard week this week, and I feel like you've just lifted a ton of bricks off my shoulders.

Even if this isn't the way he wanted the assignment, it's the way he's getting it.

Thank you thank you THANK YOU!
Last edited on
Well, Gaminic... I thought we had it. But I just talked to the teacher and I wasn't even close to what he was looking for. I've re written my code from the ground up and having some issues with my pointer to a class.

The error is occuring on line 96 "no matching function for call to 'CheckingAccount::CheckingAccount()"

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
#include <iostream>

using namespace std;

class Person
{

  string firstname;
  string lastname;
  int age;
  char gender;


  public:
    string get_firstname();
    string get_lastname();
    int get_age();
    char get_gender();
    void set_firstname(string);
    void set_lastname(string);
    void set_age(int);
    void set_gender(char);
};

void Person::set_firstname(string first_name)
{   firstname = first_name; }

void Person::set_lastname(string last_name)
{   lastname = last_name; }

void Person::set_age(int cust_age)
{   age = cust_age; }

void Person::set_gender(char cust_gen)
{   gender = cust_gen; }

string Person::get_firstname()
{   return firstname; }

string Person::get_lastname()
{   return lastname; }

int Person::get_age()
{   return age; }

char Person::get_gender()
{   return gender; }


class CheckingAccount
{

  Person p;
  int account_num;
  double balance;


  public:

    void display_account_information();
    void set_accountNumber(int);
    void set_balance(double);
    int get_accountNumber();
    double get_balance();

    CheckingAccount(int acc_num, double balance, string fn, string ln, int age, char gender);

};

CheckingAccount::CheckingAccount(int acc_num, double balance, string fn, string ln, int age, char gender)
{
acc_num = 0000000;
balance = 0.00;
fn = "unknown";
ln = "unknown";
age = 0;
gender = 'm';
}

int CheckingAccount::get_accountNumber()
{   return account_num; }

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

void CheckingAccount::set_accountNumber(int an)
{   account_num = an; }

void CheckingAccount::set_balance(double bal)
{   balance = bal; }


int main()
{
    CheckingAccount *p_ca;
    p_ca = new CheckingAccount[2];

    cout << "Hello world!" << endl;
    return 0;
}
Perhaps you should consider removing all the parameters from your constructor on lines 66 and 70? It seems that what you want there is a default constructor. If you do that, then the error on line 96 should be fixed.

-Albatross
The error was fixed but a new one "variable not declared in this scope" came up. I added the variables to the CheckingAccount class.

Is that right?

Secondly,
Now that i've got the array working, how will I assign the values to the 2 separate "checking accounts"
Topic archived. No new replies allowed.