really lost on this one issue of classes

in this program i have a class set up to gather a craigslist style post from a user and save all the information to different arrays within the class (which is also an array, to hold multiple posts in the same run) and i have another class to search the previous class and find specific types of posts.

my question is how to reference one classes private members while in the other class. i assumed it was as easy as doing class_var.member but im getting alot of weird errors and i havent been able to figure out how to do it correctly.
any help/advice is greatly appreciated

my .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
40
41
42
43
44
45
46
47
#include <iostream>
#include <cctype>
#include <cstring>

using namespace std;

class Posting            //class
{
public:
	Posting();       //constructor
	~Posting();          //destructor

	void Create_Posting(char again, bool &check);  //create a new posting of this title.
	void display_post();
	bool is_type();  //is the posting a job, housing, item for sale, or free stuff

private:
	float cost;
	char *description;
	char *title;
	char *type;
	float sqfeet;
	float rooms;
	float rent;
	char *location;
	char *email;
	int size;	
	float pay;
	float bathrooms;
};

class list
{
public:
	list();
	~list();

	void Add(char title[]);
	void display_all();
	void display_type(char type[]); //only display those postings that match a type

private:	
	char *search_type;
							//put the information about an array of postings
	int count;				//and an integer count of the number of postings available
	int size;
};

my main
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
//sean kemper
//assignment 4

#include "main.h"
using namespace std;


int main()
{
	Posting post[50];  // able to hold 50 posts 
	list list;
	char again('Y');
	bool check = false;
	int menu_choice(0);

	

	//cout << "would you like to create a post?(y/n): " << endl;
	//cin >> again;

	while(again =='y' || again == 'Y')
	{
		cout << "---Main Menu---" << endl;
		cout << "1) Create a post\n"
			 << "2) Search for a type of post\n"
			 << "3) Display all posts\n"
			 << "4) Total number of posts'\n'";

		cin >> menu_choice;
		cout << '\n';

		switch (menu_choice)
		{
		case 1:
			//create a new post

			for( int i(0); i < 50; i++)        //for loop to save more than 1 post per run
			{
				check = post[i].is_type();  // setting check equal to the bool returned by is_type
				if(check == true)
					post[i].Create_Posting(again , check);    //if check was true then we can run create_posting

				if(check == false)
					cout << "sorry that is an incorrect post type" << endl;   //otherwise we have to prompt for another post type

				cout << "\n	would you like to return to the main menu? " << endl;
				cin >> again;
				toupper (again);
			}

		case 2:
			//search for a specific type of post
			for(int i(0);i < list.count;i++)
			list.display_type(post[i].type);

		case 3:
			//display all posts
			list.display_all();

		case 4:
			//total number of posts
			cout << "There are " << list.count << "Total posts archived" << endl;
			return 0;
		}
	}
}


my search 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
#include "main.h"
using namespace std;

list::list()
{
	int size = 20;
	int count = 0;
	search_type = new char[size];
}
list::~list()
{
	delete [] search_type;
}
void list::display_type(char type[],)
{
	Posting post;

	if (search_type)
		delete [] search_type;

	cout << "which type of post doy ou want to search for? (job, housing, sale, free): " << endl;
	cin.getline(search_type, 20);
	cin.ignore(20, '\n');

	for (int i(0); i < list.count; i++)
	{

		if( strcmp (post[i].type, search_type) == 0)
		{
			cout << post[i].type << endl;
			cout << post[i].title << endl;         //This is where most of my errors stem from. referencing the variables that are in the private sector of my post class
			cout << post[i].description << endl;
			cout << post[i].email << endl;
			if (strcmp (search_type, "job") == 0)
			{
				cout << "Wage: $" << post[i].pay << "Per hour " << endl;
				cout << "Location at: " << post[i].location << endl;
			}
			if(strcmp (type, "housing") == 0)
			{
				cout << "Square feet: " << post[i].sqfeet << endl;
				cout << "Bedrooms: " << post[i].rooms <<endl;
				cout << "Bathrooms: " << post[i].bathrooms << endl;
				cout << "Rent: $" << post[i].rent << endl;
				cout << "Address: " << post[i].location << endl;
			}
			if (strcmp (type, "sale") == 0)
			{
				cout << "$" << post[i].cost << endl;
			}
		}
	}
}

void list::display_all()
{

	cout << post[i].type << endl;
	cout << post[i].title << endl;
	cout << post[i].description << endl;
	cout << post[i].email << endl;

	if (strcmp (search_type, "job") == 0)
	{
		cout << "Wage: $" << post[i].pay << "Per hour " << endl;
		cout << "Location at: " << post[i].location << endl;
	}
	if(strcmp (type, "housing") == 0)
	{
		cout << "Square feet: " << post[i].sqfeet << endl;
		cout << "Bedrooms: " << post[i].rooms <<endl;
		cout << "Bathrooms: " post[i].bathrooms << endl;
		cout << "Rent: $" << post[i].rent << endl;
		cout << "Address: " << post[i].location << endl;
	}

	if (strcmp (type, "sale") == 0)
	{
		cout << "$" << post[i].cost << endl;
	}
}



i left out my post.cpp because its rather long and there's really no errors with it. if you need to see the code to be able to help find the source of the problem i can post it
Well, you trying to access private members of another class. Private members can only be accessed by their class methods.
http://www.learncpp.com/cpp-tutorial/83-public-vs-private-access-specifiers/

Made accessor functions or (worst method actually) made classes friends.
You are accessing private member inside main() so making classes friends will not solve all your problems.
- You cannot access members(in a class) that are private by using anything other than a classes member functions.

- That's actually the point of encapsulation.
my question is how to reference one classes private members while in the other class


Asking this question implies your design might be wrong.
Topic archived. No new replies allowed.