samedi 11 février 2023

how to initialise a static member variable container with elements that are instances of structs?

Hello so I have a class that uses 5 instances of a struct and I would like a static vector to hold those instances but I do not want the elements themselves to be static.

the header file looks like this

#pragma once
#include <string>
#include<vector>

using namespace std;


struct table {
    unsigned int tableNumber;
    unsigned int numChairs;
    unsigned int numberOfPeople;
    bool occupied;

    //table();

    void toggleOccupied() {
        if (occupied == true) {
            occupied = false;
        }
        else
        {
            occupied = true;
        }
    }

    bool getTableOccupied() {
        return occupied;
    }

    void setNumChairs(int newChairNum) {
        numChairs = newChairNum;
    }

    void setTableNumber(int assignTableNum) {
        tableNumber = assignTableNum;
    }

     int getTablenumber() {
        return tableNumber;
    }

     int getNumberOfChairs() {
        return numChairs;
    }

    void setNumberOfPeople(int addNumPeople) {
        numberOfPeople = addNumPeople;
    }


};





class tableOrder {
public:

    tableOrder();
    
    int getSP() {
        return SP;
    }

    enum ServingProgress {
        seated,
        drinksOrder,
        starters,
        main,
        dessert,
        bill
    };

    ServingProgress SP = seated;

    std::string progress;

    void getCurrentProgress() {
        switch (SP) {
        case 0:
            progress = "seated";
            break;
        case 1:
            progress = "drinksOrder";
            break;
        case 2:
            progress = "starters";
            break;
        case 3:
            progress = "main";
            break;
        case 4:
            progress = "dessert";
            break;
        case 5:
            progress = "bill";
            break;
        }
    }

    void ProgressOrder() {
        switch (SP) {
        case 0:
            SP = drinksOrder;
            break;
        case 1:
            SP = starters;
            break;
        case 2:
            SP = main;
            break;
        case 3:
            SP = dessert;
            break;
        case 4:
            SP = dessert;
            break;
        case 5:
            SP = bill;
            progress = "finished";
            break;
        }
    }

    void checkForTablesInUse() {
        for (int i(0); i < allTables.size(); i++) {
            if (allTables[i].occupied) {
                TablesInUse.push_back(allTables[i]);
            }
        }
    }

    void checkForTablesNotInUse() {
        for (int i(0); i < TablesInUse.size(); i++) {
            if (TablesInUse[i].occupied == false) {
                TablesInUse.erase(TablesInUse.begin() + i);
            }
        }
    }

    void updateTablesInUse() {
        checkForTablesInUse();
        checkForTablesNotInUse();
    }

    table& getTable(unsigned int tableIndex) {
        return allTables[tableIndex - 1];
    }

    //instantiate tables
    table table_1 = { 1,6,0,false };
    table table_2 = { 2,5,0,false };
    table table_3 = { 3,4,0,false };
    table table_4 = { 4,8,0,false };
    table table_5 = { 5,4,0,false };


//protected:

    unsigned int assignedTableNumber;

    table assignedTable;

    static vector<table> availableTables;
    static vector<table> TablesInUse;
    static vector<table> TablesToSelect;
    static vector<table>  allTables;
    static tableOrder passTable;

};
    

I have made 5 instance of table and want to store them in the static allTables. Here is the cpp file

#include "TableOrder.h"

vector<table> tableOrder::availableTables = {};
vector<table> tableOrder::TablesInUse = {};
vector<table> tableOrder::TablesToSelect = {};
vector<table>tableOrder::allTables = {table_1,table_2,table_3,table_4,table_5 };

tableOrder::tableOrder() {
     assignedTableNumber = 0;

     assignedTable = table_1;
    
     
     
}

I understand why I can't initialise allTables like this as I am putting unique variables inside a shared variable. Is there any decent way around this? thanks for the help!

Aucun commentaire:

Enregistrer un commentaire