1

first time posting. Thanks in advance for your correspondence and patience.

I need to prevent the copy of an object, if that object already exists in my array.

I am beyond my depth as how to compare these values and prevent copy.

My next step is trying: unique_ptr or shared_ptr or compare() or iterator.

NO VECTORS.... I NEED TO ALLOCATE AND DE-ALLOCATE EVERYTHING.

ConfirmationSender.h

class ConfirmationSender {
    Reservation** ppCon;
    size_t cCnt;
public:
    ConfirmationSender();
    ~ConfirmationSender();
    ConfirmationSender& operator+=(const Reservation& res);
    // ConfirmationSender& operator-=(const Reservation& res);
    void display(std::ostream& os) const;
};

std::ostream& operator<<(std::ostream& os, const ConfirmationSender& obj);

ConfirmationSender.cpp

ConfirmationSender& ConfirmationSender::operator+=(const Reservation& res) {
    if (cCnt == 0) {
        ++cCnt;
        ppCon = new Reservation* [cCnt];
        ppCon[0] = new Reservation(res);
    }
    else {
        Reservation** temp1 = nullptr;
        temp1 = new Reservation * [cCnt];

        for (size_t i = 0; i < cCnt; i++) {
            temp1[i] = ppCon[i];
        }

        ++cCnt;

        delete[] ppCon;
        ppCon = new Reservation * [cCnt];

        for (size_t i = 0; i < cCnt - 1; i++) {
            ppCon[i] = temp1[i];
        }
        ppCon[cCnt - 1] = new Reservation(res);

        delete[] temp1;
        temp1 = nullptr;

        /*
        NEED TO IMPLEMENT CONDITION
        SO THE VALUE I COPY 
        CANNOT BE A VALUE ALREADY EXISTING IN ARRAY
        */
    }

    return *this;
}
class Reservation {
    std::string id, name, email;
    int seats, day, hour;

public:
    Reservation();
    Reservation(const std::string& m_res);
    void display(std::ostream& os) const;
};

std::ostream& operator<<(std::ostream& os, const Reservation& obj);
Reservation::Reservation() {
    seats = 0;
    day = 0;
    hour = 0;
};

Reservation::Reservation(const std::string& m_res) {
    std::string t_str;
    t_str = m_res;
    t_str.erase(std::remove(t_str.begin(), t_str.end(), ','), t_str.end());

    istringstream iss(t_str);
    std::string buffer = {};
    static int count = 0;

    while (iss >> buffer) {
        switch (count % 6) {
            case 0: id = buffer; break;
            case 1: name = buffer; break;
            case 2: email = buffer; break;
            case 3: seats = atoi(buffer.c_str()); break;
            case 4: day = atoi(buffer.c_str()); break;
            case 5: hour = atoi(buffer.c_str()); break;
        }
        count++;
    }
    buffer.clear();
    iss.str(std::string());
}

void Reservation::display(std::ostream& os) const {
    string menu_type;

    if (hour >= 6 && hour <= 9) { menu_type = "Breakfast"; }
    else if (hour >= 11 && hour <= 15) { menu_type = "Lunch"; }
    else if (hour >= 17 && hour <= 21) { menu_type = "Dinner"; }
    else { menu_type = "Drinks"; }

    os << "Reservation "
        << id << " "
        << name << " <"
        << email << "> "
        << menu_type << " on day "
        << day << " @ "
        << hour << ":00 for "
        << seats << " people." << '\n';
}

std::ostream& operator<<(std::ostream& os, const Reservation& obj) {
    obj.display(os);
    return os;
}

main.cpp

// Confirmation Sender
{
    std::cout << "CS: Testing Operators\n";
    std::cout << "==========================\n";
    sender1 += *ppReservations[5];
    sender1 += *ppReservations[16];
    sender1 += *ppReservations[16];
    // THE SECOND [16] SHOULD NOT BE COPIED BY ADDITION OPERATOR
    std::cout << sender1;
    std::cout << "==========================\n\n";
}
Remy Lebeau
  • 555,201
  • 31
  • 458
  • 770
  • Can you please be more specific with what your question is? It feels like you're just throwing all this code at us, lol. Please remember this isn't a homework service. – alteredinstance Oct 15 '19 at 17:43
  • 1) Please provide [mre]. If the code is not important for demonstrability of the problem: it should not be included. 2) What is the problem, exactly? "_NEED TO IMPLEMENT CONDITION SO THE VALUE I COPY CANNOT BE A VALUE ALREADY EXISTING IN ARRAY_" Why can't you iterate through the list to check if the value is already in array? – Algirdas Preidžius Oct 15 '19 at 17:46
  • 1
    1. `Reservation` should have an `==` operator that defines the rules for comparing `Reservation`s. See [What are the basic rules and idioms for operator overloading?](https://stackoverflow.com/questions/4421706/what-are-the-basic-rules-and-idioms-for-operator-overloading) for help on how to create an `==` operator. This is unfortunately about the best I can do for you without details on `Reservation`. – user4581301 Oct 15 '19 at 18:01
  • Why are you creating an array of pointers to `Reservation` objects, instead of an array of actual `Reservation` objects? – Remy Lebeau Oct 15 '19 at 18:14
  • My assignment was to: maintain a dynamically allocated array of pointers to objects of type Reservation: const sdds::Reservation** (each element of the array is a pointer to an object of type Reservation) the addition assignment operator was already declared... it was my duty to define – Heir and Reign Oct 15 '19 at 18:21
  • 1
    Since you have an array of pointers, and your `operator+=` takes a `Reservation` by reference, you can simply compare the memory addresses to know if the input `Reservation` already exists in the array, eg: `if (ppCon[index] == &res) { /* found */ }` But really, having an array of pointers doesn't make a whole lot of sense in the first place. It only makes sense if `Reservation` were a polymorphic type, but in your example it is not, so making the array hold pointers is just unnecessary overhead. – Remy Lebeau Oct 15 '19 at 19:02

0 Answers0