what I am trying to do is retrive the "status" back to my main prog of the status of a patient.

I thought it was a simple case of S.retrieve(index of person I was looking for) and I would recive the status (which is an integer) back to my main function. everytime I try to complie it will not compile. Any suggestions on how to tackle it?

Patient P; declaration of object
Sequence S; declaration of patient

I have a patient object with methods 

void patient::set_status(int aStatus)
            {
            status=aStatus;
            }

            int patient::get_status()
            {
            return status;
            }

I have a Linked list with method

  SeqItemType Sequence::retrieve(int index)
{
if ((index < 1) || (index > size())){
// can't retrieve something that isn't there
}
ListNode *cur = find(index);
return cur->item.get_status();
}

Dani AI

Generated

A short diagnosis and practical options.

The compile problem came from a mismatch between what the list actually holds and what the retrieve call tried to return. made a quick fix by changing the container element type to a primitive, which will compile but also changes what the list represents. correctly pointed out that the bounds check in the retrieve routine does nothing useful as written — it must either signal an error or return a meaningful failure result.

Three practical patterns to consider (pick one that matches the intended design):

  • Return the whole Patient (by reference) so callers can call the patient accessor themselves. This keeps the container semantically correct and avoids losing other patient data.
const Patient& Sequence::getPatient(std::size_t i) const {
    if (i >= count) throw std::out_of_range("index");
    return nodes[i]->value;
}
  • Provide a status-only accessor that indicates failure explicitly. Using std::optional makes intent clear and avoids exceptions for normal control flow.
std::optional<int> Sequence::statusAt(std::size_t i) const {
    if (i >= count) return std::nullopt;
    return nodes[i]->value.status(); // call the Patient accessor here
}
  • Use a boolean + out-parameter if exceptions/optional are not allowed by style or course rules.
bool Sequence::getStatus(std::size_t i, int &out) const {
    if (i >= count) return false;
    out = nodes[i]->value.status();
    return true;
}

Notes and references: prefer 0-based indexing with std::size_t for indices and always handle the invalid-index case (throwing std::out_of_range or returning std::nullopt/false). See the standard guidance on std::out_of_range and std::optional for patterns and tradeoffs (std::out_of_range, std::optional). If the exercise does not require a custom list implementation, a standard container such as std::vector<Patient> is simpler and well-tested (std::vector).

Recommended Answers

All 4 Replies

Is it just a typo in the post or is the object called Patient with a capital P and the class methods are using patient with a lowercase p?

I figured this one out myself or should I say the solution works!!!

Change SeqItemType to an int. then the method will return an int which is what i wanted and I can use an if/case statement from here.

SeqItemType Sequence::retrieve(int index)
{
if ((index < 1) || (index > size())){
// can't retrieve something that isn't there
}
ListNode *cur = find(index);
return cur->item.get_status();
}

What's the point of this statement? You don't return something else instead and you don't perform any logic...

if ((index < 1) || (index > size())){
// can't retrieve something that isn't there
}

Good point. We have a lab tomorrow morning and I'll ask the lecturer. Sometimes I reckon the lecturer just pulls the notes down off the net somewhere and just throws them out to us.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.