我如何允许用户确定模板类型?

时间:2019-04-15 22:18:53

标签: c++ templates if-statement linked-list switch-statement

我写了一个工作的链接队列,该链接队列以其数据类型为模板,但是用户可能以几种不同类型之一输入数据。如何选择在运行时使用哪种数据类型?

如果我分别使用每种类型,效果很好;我只需要涵盖所有可能的情况,而无需更改代码或为每种数据类型重写重载函数。

下面,我提供了代码的相关部分。如我所说,我的类成员函数没有问题。

我已经尝试过一个switch语句,该语句创建队列的x类型版本,但是不能作为稍后在switch中与其他队列数据类型“冲突”的可能性。我目前正在尝试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个错误:“ cin >> dataType”上的E0020和C2065;线,还有另一个 C2065在下一行

也许总体上有更有效的方法?我愿意接受所有建议,谢谢!

2 个答案:

答案 0 :(得分:1)

问题(问题)是当您写

initialize

您定义四个不同的 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()

答案 1 :(得分:0)

编写一个执行所需操作的模板化成员函数:

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-switch语句

然后我们可以在main中使用switch语句在它们之间进行选择:

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