-1

The code below keeps flagging an error on the head and tail in the add_ticket() function.

I cannot run the program because of these errors. Please, I need help.

I am trying to make a linked list that will take user requests and store them in a queue.

I am still kind of new to the programming world, and I am really interested in learning C++.

#include <iostream>
#include <string>
// include other header files as necessary

// constants, to define the different ticket types (TT) that may exist, e.g., whether it is a support request, bug fix, or feature request
#define TT_UNDEFINED  100 // the ticket type is undefined
#define TT_SUPPORT    101 // the ticket is for a support request
#define TT_BUG        102 // the ticket is a bug report
#define TT_FEATURE    103 // the ticket is a request for a new feature

// data structure to record details of a ticket to be placed on the queue

struct Ticket {
    std::string customer_id;
    int ticket_type; // will be assigned one of the ticket type values above, e.g., TT_BUG.
    Ticket* next;// points to next ticket in queue
    Ticket* head;
    Ticket* tail;
};

// a class to implement the linked-list data structure for the ticket queue
class TicketQueue
{
private:
    Ticket* head; // points to item at head of the queue
    Ticket* tail; // points to item at tail of the queue

public:
    TicketQueue(); // constructor, initialise the queue to be empty
    ~TicketQueue();  // destructor, free up any resources that are still in use by the queue
    void add_ticket(Ticket*, bool); // adds a new, pre-populated ticket to the queue for a premium (TRUE) or standard (FALSE) customer
    Ticket* get_next_ticket();  // remove and return (a pointer to) the next ticket from the queue for processing
    void clear(); // clears / removes all tickets from the queue
    bool is_empty(); // returns true / false whether the ticket queue is currently empty or not
    void display(); // outputs the entire queue of Tickets to cout
    void store(std::string); // outputs the entire queue of Tickets to a specified file on disk
};

// IMPLEMENTATION OF THE METHODS DECLARED IN THE TicketQueue CLASS
// your code goes here
TicketQueue::TicketQueue()
{
    head = tail = NULL;
}

TicketQueue :: ~TicketQueue()
{
    Ticket* current = head;
    Ticket* next;

    while (current != NULL)
    {
        next = current->next;
        delete current;
        current = next;
    }
}

void add_ticket(Ticket*, bool)
{
    std::string customer_Id;
    int ticket_type;
    struct Ticket* ptr;
    std::cout << std::endl
        << "Adding new Ticket\n"
        << "Enter Customer Id:\n";
    std::cin >> customer_Id;
    std::cout << "Enter Ticket Type .\n" << std::endl;
    std::cin >> ticket_type;
    ptr = new Ticket;
    ptr->customer_id = customer_Id;
    ptr->ticket_type = ticket_type;
    ptr->next = NULL;
    if (head == NULL)
        head = ptr;
    else
        tail->next = ptr;
    tail = ptr;
    std::cout << "\nNew item is inserted to the Queue!!!";
}
Remy Lebeau
  • 555,201
  • 31
  • 458
  • 770
  • 1
    C++ comes with [`std::queue`](https://en.cppreference.com/w/cpp/container/queue). No need to reinvent this wheel. – Eljay Jul 03 '21 at 12:07
  • I want to learn this way – BERNARD AIHEVBA Jul 03 '21 at 12:08
  • 1
    @BERNARD AIHEVBA This structure declaration struct Ticket { std::string customer_id; int ticket_type; // will be assigned one of the ticket type values above, e.g., TT_BUG. Ticket* next;// points to next ticket in queue Ticket* head; Ticket* tail; }; does not make a sense. Remove data members head and tail. – Vlad from Moscow Jul 03 '21 at 12:13
  • You did not declare `head` or `tail` anywhere inside of the `add_ticket()` function. Did you mean `ptr->head` and `ptr->tail`? Also, your code does not have a `main()` function defined anywhere so it won't really *run*. – Ruks Jul 03 '21 at 12:15
  • @Ruks can we chat privately? I really need some guidance on this – BERNARD AIHEVBA Jul 03 '21 at 12:24
  • 1
    Using Stack Overflow to guide you along learning C++ by refining nascent code is not a conducive way to learn C++, and not Stack Overflow's *raison d'être* because misstep-by-misstep tutorial isn't a good fit for the question-and-answer format. I recommend starting with a [good C++ book](https://stackoverflow.com/a/388282/4641116). – Eljay Jul 03 '21 at 12:35
  • You must develop new functionality *in isolation* as much as possible. (For some reason this vital idea is never mentioned in lectures or textbooks.) Write a linked list that handles `int` and get that working perfectly. Write some code for your `Ticket` struct, and get that working perfectly. Only then should you try to combine them. Advance in small steps, and *never add to code that doesn't work.* – Beta Jul 03 '21 at 12:57

1 Answers1

1

For starters the data members head and tail in the Ticket structure definition:

struct Ticket {
    std::string customer_id;
    int ticket_type; // will be assigned one of the ticket type values above, e.g., TT_BUG.
    Ticket* next;// points to next ticket in queue
    Ticket* head;
    Ticket* tail;
};

do not make any sense. Remove them. The structure should look like this:

struct Ticket {
    std::string customer_id;
    int ticket_type; // will be assigned one of the ticket type values above, e.g., TT_BUG.
    Ticket* next;// points to next ticket in queue
};

You are trying to define a stand-alone function add_ticket() instead of the member function with the same name. You need to qualify the class name, like you did with TicketQueue::TicketQueue() and TicketQueue::~TicketQueue():

void TicketQueue::add_ticket(Ticket*, bool)
{
    //...
}

Also, the function parameters are not used. So it is unclear why they are specified. It seems you meant to use the following member function declaration instead:

void add_ticket();
Remy Lebeau
  • 555,201
  • 31
  • 458
  • 770
Vlad from Moscow
  • 301,070
  • 26
  • 186
  • 335