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

How do I allow the user to determine template type?

本文关键字:类型 何允许 用户      更新时间:2023-10-16

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

如果我单独使用每种类型,它可以正常工作;我只需要涵盖所有可能性,而无需更改代码或为每种数据类型重写重载函数。

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

我已经尝试过一个创建队列的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 << "nnHow 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. integersn2. decimal numbersn3. wordsn4. charsnn";
cin >> choice;
if (choice > 0 && choice < 5)
return choice;
cout << "nnInvalid choicenn";
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 在下一行

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

问题(一个问题)是当你写的时候

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

没有更多的dataTypetheQueue可用(所有这些都超出了范围)。

我建议如下

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

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

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中使用 switch 语句在它们之间进行选择:

int main()
{
int choice = mainMenu();
cout << "nnHow 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 << "nnHow many items would you like to initially "
"populate the queue with? ";
int howMany;
cin >> howMany;
selectedOption(howMany); 
}