lundi 15 avril 2019

How do I allow the user to determine template type?

So, I have a working, templatized linked-queue.... I am simply struggling with an efficient way to allow the user to determine the data type of the queue that is used and created throughout the program... It works fine if I use each type individually... I just need every possibility covered without changing code or re-writing overloaded functions for every data type.... Here are relevant pieces of my code... As I said, no problems with my class member functions.

I already have tried a switch statement that creates x type version of the queue, but that cannot work as later possibilities within the switch "contradict" with other queue data types. I am currently trying if/else if statements, with no errors other than when I try to use the input of x type, it says it is undefined...

// From Source.cpp


    #include <iostream>
    #include <string>
    using namespace std;
    #include "LQueue.h"
    int mainMenu();
    int main()
    {
        int value;
        bool stop = false;
        Queue<int> *theQueue;
        int choice = mainMenu();

        if (choice == 1) {
            Queue<int> theQueue;
            int dataType;
        }
        else if (choice == 2) {
            Queue<double> theQueue;
            double dataType;
        }
        else if (choice == 3) {
            Queue<string> theQueue;
            string dataType;
        }
        else if (choice == 4) {
            Queue<char> theQueue;
            char dataType;
        }

        cout << "\n\nHow many items would you like to initially"
            << " populate the queue with? ";
        int howMany;
        cin >> howMany;

        for (int i = 0; i < howMany; i++)
        {
            cin >> dataType;
            theQueue.enqueue(dataType)
        }

        theQueue.display(cout);

        theQueue.dequeue();

        theQueue.display(cout);

        return 0;
    }


int mainMenu()
{
    int choice;
    cout << "What type of data will you be storing in the queue?\n"
        << "1. integers\n2. decimal numbers\n3. words\n4. chars\n\n";

    cin >> choice;
    if (choice > 0 && choice < 5)
        return choice;

    cout << "\n\nInvalid choice\n\n";
    mainMenu();
}

// Guess I'll include shown functions from the Queue class file below


    //--- Definition of enqueue()
    template <typename QueueElement> 
    void Queue<QueueElement>::enqueue(const QueueElement & value)
    {
        if (empty())
        {
            myFront = myBack = new Node(value);
        }
        else
        {
            myBack->next = new Node(value);
            myBack = myBack->next;
        }
    }

    //--- Definition of dequeue()
    template <typename QueueElement> 
    void Queue<QueueElement>::dequeue()
    {
        if (empty() == false)
        {
            Queue::NodePointer oldFront = myFront;
            myFront = myFront->next;
            delete oldFront;
        }
    }

    //--- Definition of display()
    template <typename QueueElement> 
    void Queue<QueueElement>::display(ostream & out) const
    {
        Queue::NodePointer ptr;
        for (ptr = myFront; ptr != 0; ptr = ptr->next)
            out << ptr->data << "  ";
        out << endl;

    }

    //--- Definition of front()
    template <typename QueueElement> 
    QueueElement Queue<QueueElement>::front() const
    {
        if (!empty())
            return (myFront->data);
        else
        {
            cerr << "*** Queue is empty "
                " -- returning garbage ***\n";
            QueueElement * temp = new(QueueElement);
            QueueElement garbage = *temp;     // "Garbage" value
            delete temp;
            return garbage;
        }
    }


Compiler (visual studio 2017) is showing identifier "dataType" is undefined within the following loop:


    for (int i = 0; i < howMany; i++)
        {
            cin >> dataType;
            theQueue.enqueue(dataType);
        }

2 errors: E0020 and C2065 on the "cin >> dataType;" line, and also another C2065 on the next line

Maybe there is a more efficient way overall of doing this? I am open to any and all suggestions, thank you!

Aucun commentaire:

Enregistrer un commentaire