我编写了一个工作的链接队列,它根据它的数据类型进行模板化,但是用户可能正在以几种不同的类型之一输入数据。如何选择在运行时使用哪种数据类型?
如果我单独使用每种类型,它工作正常;我只需要覆盖所有可能性,而无需更改代码或重写每种数据类型的重载函数。
下面我提供了我的代码的相关部分。正如我所说,我的班级成员功能没有问题。
我已经尝试过创建队列的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个错误:E0020和C2065上的“cin >> dataType;”线,还有下一行的另一个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 << "\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);
}
编写一个模板化成员函数,它可以执行您想要的操作:
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);
}
然后我们可以使用switch语句在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;
}
}
我们可以使用数组进行查找!
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);
}