Object inside class construction

I have a class Parking and a object inside named Menu.

I have to populate Menu through a custom constructor such as
1
2
menu1("Menu Title", int);
//int is a indentation level default to zero 

Here is my Modules

Parking Module
1
2
3
4
5
6
7
8
9
10
11
class Parking {
	private:
		char* m_fileName;
		
		Menu m_parkingMenu;
		Menu m_vehicleMenu;
        public:
		Parking(const char* fileName);		//Custom constructor
		~Parking();							//Destructor

	};

Menu Module
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
class Menu {
	private:
		char* m_title;										//Holds title
		MenuItem* m_menuItem[MAX_NO_OF_ITEMS + 1];			//Holds array of pointers to items
		int m_menuCount;									//Holds menuItems count
		double m_indentation;

		void setTitle(const char* title);

	public:
		//Class Constructors and Destructors
		Menu();
		Menu(const char* title, int indent = 0);
		~Menu();

 		Menu& operator<<(const char* item);

	};


Inside the Parking custom constructor I try and populate the menu as such
1
2
m_parkingMenu("Parking Menu, select an action:");
m_parkingMenu << "Park Vehcile" << "Return Vehcile" << "List Parked Vehicles" << "Close Parking (End of day)" << "Exit Program";

but it gives me error on the first line

Parking.cpp(22,5): error C2064: term does not evaluate to a function taking 1 arguments


I've seen online you gotta do something like this
1
2
3
4
5
6
7
8
9
10
11
12
13
class Parking {
	private:
		char* m_fileName;
		
                Parking() : m_parkingMenu("Parking Menu, select an action:") { };

		Menu m_parkingMenu;
		Menu m_vehicleMenu;
        public:
		Parking(const char* fileName);		//Custom constructor
		~Parking();							//Destructor

	};


But that gives me this error

Parking.h(24,12): error C2535: 'sdds::Parking::Parking(void)': member function already defined or declared


And i'm not sure if in future updates of the program the menu title might change based on values passed in through the Parking custom constructor, this is sort of a hard code way.

Cheers to anyone that helps!

Last edited on
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
#include <iostream>
#include <string>
#include <vector>

using std::cout;
using std::string;
using std::vector;

class Menu {
    string m_title;
    vector<string> m_items;
public:
    Menu(const string& title, const vector<string>& items)
        : m_title(title), m_items(items) { }
    void dump() const;
};

void Menu::dump() const {
    cout << m_title << '\n';
    for (const auto& item: m_items)
        cout << '\t' << item << '\n';
}

class Parking {
    Menu m_menu;
public:
    Parking(const string& title, const vector<string>& items)
        : m_menu(title, items) { }
    void dump() const { m_menu.dump(); }
};

int main() {
    Parking parking(
        "Parking",
        {
            "One",
            "Two",
            "Three"
        }
    );
    parking.dump();
}

@dutch thanks for the help, but I have guidelines of how to implement my design.

This is the client code
1
2
3
4
5
6
7
8
9
int main() {
   Parking P("ParkingData.csv"), bad1(nullptr),bad2("");
   bad1.run();
   bad2.run();
   P.run();
   std::cout << "@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@" << std::endl;
   P.run();
   return 0;
}

Can't use vectors or strings either, trust me I wish I could, this is for a school assignment.

I just need a way to call my own custom constructor for my Menu objects inside my Parking object constructor.
I tried explicitly calling the custom constructor but it doesn't work, soon as I leave the scope I called it in, they get destroyed.
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
Parking::Parking(const char* fileName) {
		if (fileName && fileName[0] != '\0') {
			//Allocate and copy new fileName
			m_fileName = new char[strlen(fileName) + 1];
			strcpy(m_fileName, fileName);

			//load data file
			if (loadDataFile()) {
				//Populate both menu
				m_parkingMenu.Menu::Menu("Parking Menu, select an action:");
				m_parkingMenu << "Park Vehicle" << "Return Vehicle" << "List Parked Vehicles" << "Close Parking (End of day)" << "Exit Program";
				
				m_parkingMenu.Menu::Menu("Select type of the vehicle:", 1);
				m_vehicleMenu << "Car" << "Motorcycle" << "Cancel";


			}
			else {	//Loading data failed, output error
				std::cout << "Error in data file" << std::endl;
			}
		}
		else {	//fileName invalid, set to empty state, output file
			std::cout << "Error in data file" << std::endl;
			m_fileName = new char[1];
			m_fileName[0] = '\0';
		}
	}
Please feel free to come up with your own solutions, I cant use my Menu Modules assignment operator because I need to be able to change my indentation.

Their has to be a way to call a custom constructor, I'm okay if i have to hard code it in via a header file. Just has to work.
Topic archived. No new replies allowed.