Constructors and arrays

I'm currently in school in my second c++ class. (ADVANCED C++)
To be quite honest, these courses are terrible. Learn more on youtube. Anyways, I'm struggling with this topic.

I need a custom constructor, some data, menus, etc. Just how I have it laid out in the code. I'm having an error populate (ALL THE CODE IS COMMENTED).

Not sure where to begin, and i'm not entirely sure I understand how this works with storing objects in an array.

Any edits would be awesome and information a beginner could understand or even resources :) Thank you!

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

using namespace std;

class ContactsClass
{
    //private variables only this class can see.
    private:
	string recordNumber;
	string fn;
	string ln;
	string age;
	string phone;
	
	//public constructor and functions the entire program can see.
	//assignment requires a custom constructor. Recieving error: no matching function for call to 'ContactsClass::ContactsClass()' (main.cpp:59:25)
	public:
	    ContactsClass(string aRn, string aFn, string aLn, string aAge, string aPhone){
	        recordNumber = aRn;
	        fn = aFn;
	        ln = aLn;
	        age = aAge;
	        phone = aPhone;
	    }
	    
	    //Get Information functions.
		void getRecord(){
			getline( cin, recordNumber );
		}
		void getFirstName(){
		    getline( cin, fn );
		}
		void getLastName(){
		    getline( cin, ln );
		}
		void getAge(){
		    getline( cin, age );
		}
		void getPhone(){
		    getline( cin, phone);
		}
		
		//show Information function.
		void displayInfo()
		{
			cout << "Record Number: " << recordNumber << endl;
			cout << "First Name: " << fn <<endl;
			cout << "Last Name: " << ln << endl;
			cout << "Age: " << age << endl;
			cout << "Phone Number: " << phone <<endl;
		}
};


int main()
{
    //Basic Array
    ContactsClass list[5]; // <<<<<-------------------------- THIS IS WHERE THE ERROR POINTS TO 
    //Variables
    string add;
    string exit;
    int choice;
    int i = 0;
    
    do{
        //Main Menu - run this until choice 3 is picked.
        cout << "1. Add New Record" << endl;
        cout << "2. Display all records" << endl;
        cout << "3. Exit Program" << endl;
        cout << "Enter Menu Number:" << endl;
        cin >> choice;
        cin.ignore();
        
        switch(choice){
            case 1:
                //Enter information
	            do{
	            	cout << "Enter record number: ";
	            	list[i].getRecord();
		            cout << "Enter First Name: ";
            		list[i].getFirstName();
            		cout << "Enter Last Name: ";
            		list[i].getLastName();
            		cout << "Enter Age: ";
            		list[i].getAge();
            		cout << "Enter Phone Number: ";
            		list[i].getPhone();
            		//increment index.
            		i++;
            		//if max is reached - break.
            		if(i >= 2){
            		    break;
            		}
            		else{
            		    //allow user to add new records.
            		    cout << "Add another record? Enter 'yes' or 'no': ";
            		    cin >> add;
            		    cin.ignore();
            		        if(add == "no"){
            		            break;
            		        }
            		    }
	            }while(add == "yes");
	            //breaks to main menu.
	            break;
	            
	        case 2:
	            //record information display.
	            //header
	            cout << "    Record Information    " << endl;
	            cout << "--------------------------" << endl;
	            //loop to cycle through all records and display.
	            for( int i=0; i<3; i++ ){
		            list[i].displayInfo();
		            cout << "--------------------------" << endl;
	            }
	            //user input for exit.
	            cout << "Enter 'exit' to return to main menu."<<endl;
	            cin >> exit; 
	            cin.ignore();
        }
        
    }while(choice != 3);
    
	

	
	return 0;
}
Hello tdilley28,

When you create a class the compile will provide a default ctor and dtor. Something like ClassName(){}.

When you create your own overloaded ctor the compiler no longer will provide a default ctor. You will have to.

In line 59 you are creating an array of classes looking for a default ctor that does not exist.

Either provide a default ctor or initialize each element of the array with something that the overloaded ctor can use. Even if it is empty strings or zero values for numeric variables.

Andy
At line 56, you're creating an array of five ContactsClass objects. This means 5 ContactsClass objects are being created. This means that the default constructor ContactsClass() is being invoked 5 times.

But you haven't defined a default constructor. And since you have defined another constructor, the compiler won't create a default constructor for you.

You'll need to define a default constructor for ContactsClass.

The stuff school doesn't teach ya! I looked at the lesson content, shows nothing of the sort. Very strange. Also doesn't discuss arrays. I Fixed it I think! :) Thank you so much for your help on this everyone! Your information allowed me to search youtube, found a good video that literally explains this by searching default constructors. Couldn't find this video earlier searching for just classes and objects.

Here's the video I found:
https://www.youtube.com/watch?v=0Gcy49Gin_k


New code that works: Figured I'd update it in case anyone else needs this info.

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

using namespace std;

class ContactsClass
{
    //private variables only this class can see.
    private:
	string recordNumber;
	string fn;
	string ln;
	string age;
	string phone;
	
	//public constructor and functions the entire program can see.
	public:

	    ContactsClass(){
	        recordNumber = "0000";
	        fn = "First Name";
	        ln = "Last Name";
	        age = "Age";
	        phone = "Phone Number";
	    }
	    ContactsClass(string aRn, string aFn, string aLn, string aAge, string aPhone){
	        recordNumber = aRn;
	        fn = aFn;
	        ln = aLn;
	        age = aAge;
	        phone = aPhone;
	    }
	    
	    //Get Information functions.
		void getRecord(){
			getline( cin, recordNumber );
		}
		void getFirstName(){
		    getline( cin, fn );
		}
		void getLastName(){
		    getline( cin, ln );
		}
		void getAge(){
		    getline( cin, age );
		}
		void getPhone(){
		    getline( cin, phone);
		}
		
		//show Information function.
		void displayInfo()
		{
			cout << "Record Number: " << recordNumber << endl;
			cout << "First Name: " << fn <<endl;
			cout << "Last Name: " << ln << endl;
			cout << "Age: " << age << endl;
			cout << "Phone Number: " << phone <<endl;
		}
};


int main()
{
    //Basic Array
    ContactsClass list[5]; 
    //Variables
    string add;
    string exit;
    int choice;
    int i = 0;
    
    do{
        //Main Menu - run this until choice 3 is picked.
        cout << "1. Add New Record" << endl;
        cout << "2. Display all records" << endl;
        cout << "3. Exit Program" << endl;
        cout << "Enter Menu Number:" << endl;
        cin >> choice;
        cin.ignore();
        
        switch(choice){
            case 1:
                //Enter information
	            do{
	            	cout << "Enter record number: ";
	            	list[i].getRecord();
		            cout << "Enter First Name: ";
            		list[i].getFirstName();
            		cout << "Enter Last Name: ";
            		list[i].getLastName();
            		cout << "Enter Age: ";
            		list[i].getAge();
            		cout << "Enter Phone Number: ";
            		list[i].getPhone();
            		//increment index.
            		i++;
            		//if max is reached - break.
            		if(i >= 2){
            		    break;
            		}
            		else{
            		    //allow user to add new records.
            		    cout << "Add another record? Enter 'yes' or 'no': ";
            		    cin >> add;
            		    cin.ignore();
            		        if(add == "no"){
            		            break;
            		        }
            		    }
	            }while(add == "yes");
	            //breaks to main menu.
	            break;
	            
	        case 2:
	            //record information display.
	            //header
	            cout << "    Record Information    " << endl;
	            cout << "--------------------------" << endl;
	            //loop to cycle through all records and display.
	            for( int i=0; i<3; i++ ){
		            list[i].displayInfo();
		            cout << "--------------------------" << endl;
	            }
	            //user input for exit.
	            cout << "Enter 'exit' to return to main menu."<<endl;
	            cin >> exit; 
	            cin.ignore();
        }
        
    }while(choice != 3);
    
	

	
	return 0;
}
I've made some changes (eg initialiser list, pass by ref rather than value etc). Also, I've renamed the 'get...' functions as 'input...' as 'get...' is usually used to return info from the class.

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

using namespace std;

class ContactsClass {
private:
	string recordNumber;
	string fn;
	string ln;
	string age;
	string phone;

public:

	ContactsClass() = default;
	ContactsClass(const string& aRn, const string& aFn, const string& aLn, const string& aAge, const string& aPhone) : recordNumber(aRn), fn(aFn), ln(aLn), age(aAge), phone(aPhone) {}

	//Get Information functions.
	void inputRecord() {
		getline(cin, recordNumber);
	}

	void inputFirstName() {
		getline(cin, fn);
	}

	void inputLastName() {
		getline(cin, ln);
	}

	void inputAge() {
		getline(cin, age);
	}

	void inputPhone() {
		getline(cin, phone);
	}

	void displayInfo() {
		cout << "Record Number: " << recordNumber << endl;
		cout << "First Name: " << fn << endl;
		cout << "Last Name: " << ln << endl;
		cout << "Age: " << age << endl;
		cout << "Phone Number: " << phone << endl;
	}
};

int main()
{
	const size_t maxcon {5};

	ContactsClass list[maxcon];
	size_t choice {};

	do {
		//Main Menu - run this until choice 3 is picked.
		cout << "1. Add New Record\n";
		cout << "2. Display all records\n";
		cout << "3. Exit Program\n";
		cout << "Enter Menu Number:\n";
		cin >> choice;
		cin.ignore();

		switch (choice) {
			case 1:
			{
				size_t i {};
				string add;

				//Enter information
				do {
					cout << "Enter record number: ";
					list[i].inputRecord();

					cout << "Enter First Name: ";
					list[i].inputFirstName();

					cout << "Enter Last Name: ";
					list[i].inputLastName();

					cout << "Enter Age: ";
					list[i].inputAge();

					cout << "Enter Phone Number: ";
					list[i].inputPhone();

					if (++i < maxcon) {
						cout << "Add another record? Enter 'yes' or 'no': ";
						cin >> add;
						cin.ignore();
					} else
						add = "no";

				} while (add == "yes");
			}
			break;

			case 2:
			{
				string exit;

				//record information display.
				//header
				cout << "    Record Information    \n";
				cout << "--------------------------\n";
				//loop to cycle through all records and display.
				for (int i = 0; i < maxcon; ++i) {
					list[i].displayInfo();
					cout << "--------------------------\n";
				}

				//user input for exit.
				cout << "Enter 'exit' to return to main menu.\n";
				cin >> exit;
				cin.ignore();
			}
			break;

			case 3:
				break;

			default:
				cout << "Invalid option\n";
				break;
		}

	} while (choice != 3);
}

Topic archived. No new replies allowed.