Multiple class problems (object initialization)

I'm having trouble initializing the object "com" in my main .cpp and can't find out what's wrong, I'm getting an access violation (like i'm trying to access something private.) If you could help me out, I'd be ever appreciative..

Naturally if you comment out the committeeClass com it works just fine.

------------------------------
main.cpp
------------------------------
1
2
3
4
5
6
7
8
9
10
11
12
13
#include <iostream>
#include <string>
#include "committee.h"
#include "menu.h"
using namespace std;

int main()
{
	committeeClass com;
	Menu menu1;
	menu1.executeMenu();
	return 0;
};

-------------------------------
committee.h
--------------------------------
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
//File: committee.h
//Author: Dakota Irsik
//This file contains the declaration of class 'committeeClass'

#include <iostream>
#include <string>

using namespace std;

class committeeClass
{
	private: 
		int* cId;			//Committee Number
		string* cName;		//Committee Name
		string* cPurpose;	//Committee Purpose

	public:

		committeeClass();									//default constructor 

-------------------------------------
commiteeClass.cpp
-------------------------------------
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
//File: committeeClass.h
//Author: Dakota Irsik
//This file contains the impliementation of class 'committeeClass'

#include "committee.h"
#include <iostream>

using namespace std;

//default constructor

committeeClass::committeeClass()
{
	cId = 0;
	*cName = " ";
	*cPurpose = " ";
}

-----------------------------------
menu.h
----------------------------------
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
#include <list>
#include <iostream>
using namespace std;

#ifndef M_H
#define M_H

class Menu
{
public:
	//Read in all committee and faculty information from files and store
	//all information into a appropriate sorted list.
	Menu();
	
	//Call appropriate functions to print and execute Menu options 
	void executeMenu(); 

private:
	//Print a menu for the user
	void printMenu();
 
};
#endif 

-------------------------
menuClass.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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
//File: menuClass.cpp
//Author: Dakota Irsik
//This file contains the impliementation of class menuClass

#include "menu.h"
#include "committee.h"
#include <iostream>
#include <string>
#include <iomanip> 
#include <fstream> 
#include <list>
#include <queue> 

using namespace std;



Menu::Menu()
{
	cout << "made it into Menu constructor" << endl;
	int cIdin;
	string cNamein, cPurposein;
	//committeeClass com;
	//list<committeeClass> newlist;
	//list<committeeClass>::iterator p;
	
	
	
	ifstream fil;
	fil.open("committee.txt");

	if( fil.fail() )
		cout << "File didn't open.";
	else
		cout << "Successfully entered file. " << endl;

	//p = newlist.begin();
	while (!fil.eof())
	{

		fil >> cIdin;
		//com.setcId(cIdin);

		fil.ignore();
		getline(fil,cNamein);
		//com.setcName(cNamein);

		getline(fil,cPurposein);
		//com.setcPurpose(cPurposein);
	//	newlist.insert(p, com);

		//cout << com << endl;

	}

}

void Menu::executeMenu()
{
	cout << "made it into executeMenu function" << endl;
	int selection = 0;
	printMenu();
	cin >> selection;
	bool done = false;
		while (!done)
		{
			switch (selection)
			{
				case 1:
					void printCommittees();
				case 2:
					void addItem();
				case 3:
					void deleteItem();
				case 4:
					void retrieveItem();
				case 5:
					done;
				default:
					cout << "Invalid choice." << endl;
			}

		}
}

void Menu::printMenu()
{

	cout << "1. Print List of Committees" << endl;
	cout << "2. Add Item" << endl;
	cout << "3. Delete Item" << endl;
	cout << "4. Retrieve Item" << endl;
	cout << "5. Quit" << endl;
	
}
Last edited on
In commiteeClass.cpp, you are using string pointers without allocating space. Pointers must point to some allocated memory location before you can use the * operator on them to extract the value. What you are doing reduces to the following.
1
2
string *cName;
*cName = " "; // This should result in a segmentation fault. 


What you need in your constructor is the following
1
2
3
4
5
6
committeeClass::committeeClass():cId(new int), cName(new string), cPurpose(new string)
{
        *cId = 0;
	*cName = " ";
	*cPurpose = " ";
}


followed with a destructor with frees the allocated space.
Last edited on
Topic archived. No new replies allowed.