Storing objects into an array

Im trying to store objects of type MapEntry in to an array that is in another class but i'm not quite sure on how to approach it. Specifically because the array is private. Can anyone be of any assistance ?



1
2
3
4
5
6
7
8
9
10
11
12
13
14

int main()
{

Map theMap;


MapEntry myMap("Course","001");
MapEntry myMap3("Course","150");
//store all these objects into an array 

}





This is the object I am trying to store it in. All of the objects have to go into the array mapping[MAX_MAPPINGS].



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

	class Map {
	private:
		static const int MAX_MAPPINGS = 100;
		MapEntry mapping[MAX_MAPPINGS];
		size_t numberOfMappings;

		// Performs an ascending sort by default. If ascending is false,
		// then it performs a descending sort. The actual sorting is performed
		// on the key.
		void SelectionSort(bool ascending = true);

		// Returns the index of the next smallest key.
		int GetIndexOfNextSmallest(const size_t start) const;

		// Returns the index of the next largest key.
		int GetIndexOfNextLargest(const size_t start) const;

		// Swaps the two elements whose indices are provided.
		void SwapElementsAtIndex(const int index1, const int index2);

	public:
		// Initializes the object by setting numberOfMappings to 0.
		Map();

		// Retrieves the value for the given key. If the key is not found,
		// then the value is undefined (should not be used by caller), and
		// method returns false. If found, method returns true.
		bool GetValueAssignedToKey(const string& theKey, string& theValue);

		// Sets the value for the given key. The new pair is added to the end
		// of the mappings array. Keys must be unique, so if a duplicate key
		// is given, raise a MapException with the message:
		// "MapException: Duplicate keys are not allowed.".
		void SetValueForKey(const string& theKey, const MapValueType& theValue);

		// Sorts the mappings array in ascending order by default. If ascending
		// is false, then it sorts in descending order. Calls the private method
		// SelectionSort() to perform the actual sort.
		void Sort(bool ascending = true);

		// List all the key-value pairs using the format:
		// Key = <key>,<space>Value = <value>
		void List() const;
	};// end Map
If it's private, you'll need some sort of function to insert into it. The std::map lets you use operator[] for this purpose.
The only solution I can think of at the moment is using a friend function. Is there anything else you would suggest ?
You could add a public member function Add(const MapEntry& entry) to your Map class:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
	class Map {

	// stuff ...

	private:

	// stuff ...

	public:

	// stuff ...

	void Add(const MapEntry& entry)
	{
		// copy entry into array mapping here ...
	}

	// stuff ...
	}
Topic archived. No new replies allowed.