如何允许用户确定模板类型?
How do I allow the user to determine template type?
我编写了一个工作的链接队列,该队列在其数据类型上模板化,但是用户可能以几种不同的类型之一输入数据。如何选择在运行时将使用的数据类型?
如果我单独使用每种类型,它可以正常工作;我只需要涵盖所有可能性,而无需更改代码或为每种数据类型重写重载函数。
下面我提供了我的代码的相关部分。正如我所说,我的类成员函数没有问题。
我已经尝试过一个创建队列的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);
没有更多的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 << "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);
}
相关文章:
- 为什么在我的函数类型后使用引用运算符 (&) 允许我修改它返回的值?
- 为什么 c++(g++) 不允许模板返回类型和函数名称之间有空格?
- 如何允许模板参数中的类类型,仅当它有两个基类时
- 在 C++20 中是否不再允许在 std 中对程序定义类型的函数模板进行专用化?
- C++从外部类继承的嵌套类;不允许使用不完整的类型
- 当目标指针不是基类的类型时,为什么允许dynamic_cast为多态类生成 null 指针?
- 标准对此指向成员函数类型模板参数有何说明?是我的代码有误,还是 MSVS 16.6 有问题?
- 为什么允许dynamic_cast到非唯一的基类类型?
- 如何在C++中允许成员函数的自定义返回类型进行类型擦除?
- E0322:不允许使用抽象类类型 " " 的对象
- 类型别名允许分配任意指针,尽管 int* 是必需的
- 奇怪的不完整类型不允许错误
- STD ::向量迭代器类型和允许的操作
- 不完整的类型不允许错误
- 为什么显式类型转换允许对私有继承进行向上转换
- c++11 中的类型何时允许内存
- 如何从输入字符串中分离整数并将其转换为int类型以允许对其进行计算
- 非类型哪些类型是允许的
- c++不完全类型不允许在类内部使用
- 不完全类型不允许出现错误和元组