c++ using a member function to place one object inside another

So I have two classes UnsortedType and Appoinment.

Appointment accepts user input as "1/23/1999 Coffee" which would result in 1 as the month 23 as the day and 1999 as the year along with Coffee as the description.

UnsortedType just stores a list of these Appoinment objects.

In main after storing user input into an appoinment object, I want to add it to the list via method PutItem. I do this as shown below(part of main.cpp):

1
2
3
4
5
6
{
    cout << "Add the date and description of your appointment:";
    cin >> a; //storing user input in Appointment a
    x.PutItem(a); //attempting to store Appointment A in UnsortedType x
}



However, this does not seem to work as it says class "Appoinment" has no member "PutItem"

Any ideas on fix, or on how I can accomplish my goal?







unsorted.h
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
  #ifndef LAB3_H
#define LAB3_H 
#include <iostream>
#include <string>
#include <cstdlib>
#include <iomanip>
#include "Appointment.h"
#include <vector>


using namespace std;

//class Appointment; // forward declaration

class UnsortedType
{
    private:
		int length = 0;
		int size;
	   //ItemType* item_arr;
		Appointment* item_arr;
		

	
    public:
		UnsortedType();
		UnsortedType(int num);
		UnsortedType(const UnsortedType& arg); // copy constructor
		UnsortedType& operator = (const UnsortedType& arg); // assignment operator
                ~UnsortedType(); //destructor
		int getLength() const { return length; };
	        Appointment* getitem_arr() const { return item_arr; };
		void PutItem(Appointment s);

};



#endif 


unsorted.cpp
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
#include <string>
#include <cstdlib>
#include "unsorted.h"
//#include "Appointment.h"
#include <iomanip>
#include <vector>

using namespace std;

UnsortedType::UnsortedType()
{
	//item_arr = new ItemType[10];
	item_arr = new Appointment[10];
	size = 10;
}

UnsortedType::UnsortedType(int num)
{
	//item_arr = new ItemType[num];
	item_arr = new Appointment[num];
	size = num;
}

UnsortedType::~UnsortedType()
{
	delete[] item_arr;
}

UnsortedType::UnsortedType(const UnsortedType& arg)
{
	length = arg.getLength();
	item_arr = new Appointment[length + 1];
	int i = 0;
	for (i = 0; i < length; i++)
		item_arr[i] = arg.item_arr[i];
}

UnsortedType& UnsortedType::operator = (const UnsortedType& arg)
{
	length = arg.getLength();
	//item_arr = new ItemType[length + 1];
	item_arr = new Appointment[length + 1];
	int i = 0;
	for (i = 0; i < length; i++)
		item_arr[i] = arg.item_arr[i];
	return *this;
}


void UnsortedType::PutItem(Appointment s)
{
        Appointment* temp_arr;
	bool x = isFull();
	if (x == false)
	{
		item_arr[length] = s;
		length = length + 1;
	}
	else
	{
		//temp_arr = new ItemType[size * 2];
		temp_arr = new Appointment[size * 2];
		for (int i = 0; i < size; i++)
		{
			temp_arr[i] = item_arr[i];
		}

		delete[] item_arr;
		item_arr = temp_arr;
		size = size * 2;
		item_arr[length + 1] = s;
		length = length + 1;
	}

}

  


Appointment.h
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
  #ifndef LAB3_H2
#define LAB3_H2
#include <iostream>
#include <string>
#include <cstdlib>
#include <iomanip>
#include "unsorted.h"
#include <vector>


using namespace std;

class Appointment 
{
	
	int year;
	int month;
	int day;
	int length = 0;
	string desc;
	string checker;
	

public:
	Appointment();
	Appointment(int num, int num1, int num2, string a);
	int getYear() const { return year; };
	int getMonth() const { return month; };
	int getDay() const { return day; };
	int getLength() const { return length; };
	string getDesc() const { return desc; };
	string getChecker() const { return checker; };
	friend istream& operator >> (istream& ins, Appointment& arg);
	friend ostream& operator << (ostream& out, const Appointment& arg);

#endif 


Appointment.cpp
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
#include <string>
#include <cstdlib>
#include "unsorted.h"
#include "Appointment.h"
#include <iomanip>
#include <vector>

using namespace std;

Appointment::Appointment()
{
	day = 0;
	month = 0;
	year = 0;
	desc = "";
}

Appointment::Appointment(int num, int num1, int num2, string a)
{
	day = num;
	month = num1;
	year = num2;
	desc = a;
}

istream& operator >> (istream& ins, Appointment& arg)
{
	Appointment temp;
	string s, x, y;

	label1:
	getline(cin, s);
	
	if (arg.getChecker() == s)
	{
		cout << "Error! Appointment has already been entered.";
		cout << "Add the date and description of your appointment:";
		goto label1;

	}

	
	int index = s.find(" ");
	x = s.substr(0, index);
	y = s.substr(index, s.length());
	arg.desc = y;

	if (x.length() == 9)
	{
		arg.day = stoi(x.substr(2, 2).c_str());
		arg.month = stoi(x.substr(0, 1).c_str());
		arg.year = stoi(x.substr(5, 4).c_str());
	}
	else
	{
		arg.day = stoi(x.substr(3, 2).c_str());
		arg.month = stoi(x.substr(0, 2).c_str());
		arg.year = stoi(x.substr(6, 4).c_str());
	}

    
	arg.length = arg.getLength() + 1;

	
	arg.checker = s;
	cin.clear();
	return ins;
}
ostream& operator << (ostream& out, const Appointment& arg)
{
	out << arg.getDay() << "/" << arg.getMonth() << "/" << arg.getYear() << " " << arg.getDesc();

	return out;
}

  


main.cpp
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
#include <string>
#include <cstdlib>
#include "unsorted.h"
#include "Appointment.h"
#include <iomanip>
#include <vector>

using namespace std;

int main()
{
	int n = 1;
	string command;

	Appointment a;

	UnsortedType x;
	
	do
	{
        cout << "Choose a command (Display, Add, Search, Delete, Quit): ";
		cin >> command;

		if (command == "Add" || command == "add")
		{   
			
			cout << "Add the date and description of your appointment:";
			cin >> a;
			x.PutItem(a);
        }

		
	return 0;
}
volapiik wrote:
it says class "Appoinment" has no member "PutItem".
Any ideas on fix


You could write a member function called PutItem for class Appointment.
my assignment requires PutItem to be a part of UnsortedType.
Also I don't understand the error message since I am using the member function using the object x which is UnsortedType so there shouldn't be an issue.
Last edited on
for anyone curious the solution was I accidentally had the x object initialized as both Appointment and UnsortedType in main. Solution is to remove Appointment x.
Topic archived. No new replies allowed.