Как мне разрешить пользователю определять тип шаблона? - PullRequest
1 голос
/ 16 апреля 2019

Я написал рабочую связанную очередь, которая основана на его типе данных, однако пользователь может вводить данные в одном из нескольких различных типов.Как выбрать тип данных, который будет использоваться во время выполнения?

Это прекрасно работает, если я использую каждый тип отдельно;Мне просто нужно охватить каждую возможность без изменения кода или перезаписи перегруженных функций для каждого типа данных.

Ниже я предоставил соответствующие фрагменты своего кода.Как я уже сказал, никаких проблем с моими функциями-членами класса.

Я уже пробовал оператор switch, который создает версию очереди типа x, но он не может работать, так как более поздние возможности внутри коммутатора "противоречат" другой очередитипы данных.В настоящее время я пытаюсь использовать операторы if / else if, без ошибок, кроме тех случаев, когда я пытаюсь использовать ввод типа x, он говорит, что он не определен.

// 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:
```c++

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

2 ошибки: E0020 и C2065 в "cin >> dataType;"линия, а также еще один C2065 на следующей строке

Может быть, есть более эффективный способ сделать это в целом?Я открыт для любых предложений, спасибо!

Ответы [ 2 ]

1 голос
/ 16 апреля 2019

Проблема (проблема) в том, что когда вы пишете

    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;
    }

, вы определяете четыре разные theQueue и четыре разные dataType переменные, каждая из которых действительна только внутри соответствующего телаif.

Итак, когда вы пишете

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

    theQueue.display(cout);

    theQueue.dequeue();

    theQueue.display(cout);

, больше нет доступных dataType и theQueue (все они находятся вне области видимости).

Я предлагаю следующее:

    if (choice == 1) {
        foo<int>();
    }
    else if (choice == 2) {
        foo<double>();
    }
    else if (choice == 3) {
        foo<std::string>();
    }
    else if (choice == 4) {
        foo<char>();
    }

, где foo() - это почти такая же функция шаблона (осторожно: код не проверен)

template <typename T>
void foo ()
 {
   Queue<T> theQueue;
   T        dataType;

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

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

   theQueue.display(cout);

   theQueue.dequeue();

   theQueue.display(cout);
 }
0 голосов
/ 16 апреля 2019

Напишите шаблонную функцию-член, которая делает то, что вы хотите:

template<class DataType>
void processInput(int howMany) {
    DataType value;

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

    theQueue.display(cout);

    theQueue.dequeue();

    theQueue.display(cout);
}

Метод 1 - оператор переключения

Затем мы можем использовать оператор переключения для выбора между ними в main:

int main()
{
    int choice = mainMenu();

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

    switch(choice) {
      case 1:
        processInput<int>(howMany);
        break;
      case 2:
        processInput<double>(howMany);
        break;
      case 3:
        processInput<string>(howMany);
        break;
      case 4:
        processInput<char>(howMany);
        break;
    }
}

Метод 2 - массив методов

Мы можем использовать массив для поиска!

using func_t = void(*)(int);

int main() {

    std::vector<func_t> options = {
        processInput<int>, 
        processInput<double>, 
        processInput<string>, 
        processInput<char>
    };

    int choice = mainMenu();

    func_t selectedOption = options[choice - 1]; 

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

    selectedOption(howMany); 
}
...