Originally Posted by
anon
Unless you are going to tell what you are trying to achieve any advice is as good as any other. Make nicefoo and badfoo free functions (perhaps restricted to the file B is implemented in). Or make them methods of B instead (what is the purpose of them being private non-static members of A if they apparently won't use any data from A anyway). Etc.
Yes, technically you either need an instance of A, or they would have to be static member functions. Which you choose (if not one listed above) naturally depends on what they are all for.
Code:
// Class menu
#ifndef MENU_H
#define MENU_H
#include <iostream>
#include <string>
#include "MenuItem.h"
class Menu
{
private:
struct MenuNode {
MenuNode *next;
MenuItem *item;
};
MenuNode *head;
MenuNode *tail;
public:
Menu();
~Menu();
void addMenu(std::string s, char c, MenuFunc f);
void choose(MenuContext &ctxt);
// Menu operation functions
static void Exit(MenuContext &ctxt);
static void AddCarOption(MenuContext &ctxt);
static void EditCarOption(MenuContext &ctxt);
static void ListCarsOption(MenuContext &ctxt);
static void ReadFileOption(MenuContext &ctxt);
static void SearchCarOption(MenuContext &ctxt);
static void UpdateCarOption(MenuContext &ctxt);
static void DeleteCarOption(MenuContext &ctxt);
static void TitleDescription(MenuContext &ctxt);
};
Code:
// Class Fleet : Handles all Fleet functions to process the fleet data
#ifndef FLEET_H
#define FLEET_H
#pragma once
#include <vector>
#include "Car.h"
#include "Menu.h"
#define MAX_PATH 50
// Class Fleet : Acts as a Main controller
using namespace std;
typedef struct {
string make;
string model;
} CAR_MAKE;
class Fleet
{
public:
friend class Menu;
Fleet();
void RunFleet();
private:
typedef vector<Car> CarFleet;
typedef CarFleet::iterator FleetIterator;
CarFleet getFleet();
void SortAlpha();
void AddNewCar();
void DeleteCar();
void ShowFleetList();
void CreateContract();
void UpdateFleetFile();
void ReadFleetFromFile();
void EditFleetContract();
FleetIterator find(FleetIterator&);
Car& matchRightCar(FleetIterator iter);
private:
CarFleet fleet;
};
#endif
Menu use fleet functions which are private...
i.e.
Code:
// Menu Function : To add new car to fleet list
void Menu::AddCarOption(MenuContext &ctxt)
{
Fleet operate;
Clrscr();
cout << "\t\t\t\tÉÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍ»\n";
cout << "\t\t\t\tº NEW CAR º\n";
cout << "\t\t\t\tÈÍÍÍÍÍÍÍÍÍÍÍÍÍÍͼ\n";
operate.AddNewCar(); //HERE <---
Pause();
}
At the same time, Fleet use Menu functions to generate a list of Menu
i.e.
Code:
void Fleet::RunFleet()
{
Menu m;
MenuContext ctxt;
// Add menu items to the List
Clrscr();
m.addMenu("Description of Program", '1', TitleDescription);
m.addMenu("Read Fleet from File", '2', ReadFileOption);
m.addMenu("Display List of cars", '3', ListCarsOption);
m.addMenu("Search Car: [Model+Make]", '4', SearchCarOption);
m.addMenu("Add new car details", '5', UpdateCarOption);
m.addMenu("Save Fleet File", '6', AddCarOption);
m.addMenu("Edit Fleet", '7', EditCarOption);
m.addMenu("Delete Car", '8', DeleteCarOption);
m.addMenu("Main Menu", '9', Exit);
while (!ctxt.fquit) {
m.choose(ctxt);
}
}
All these functions from TitleDescription to Exit are static members of Menu... Compiler complains that Fleet can't see them, even if i say m.Exit ??
SO! Both functions uses each others member functions, but am not wining in accessing static members from Menu in Fleet