稍后在程序中使用if-else构造内部声明的变量会导致未声明的标识符错误

Using variables declared inside an if else construct later in the program results in an undeclared identifier error

本文关键字:变量 声明 错误 标识符 未声明 内部 程序 if-else      更新时间:2023-10-16

这个程序非常不言自明,所以我不会真正讨论它的用途。

我现在的主要问题是在第82、89、95和101行,我在编译时遇到了"arr"answers"input"的"未声明标识符"错误。

这是因为我在if-else-if构造中声明了它们吗?如果是,有什么方法可以绕过它吗。感谢您提前提供的帮助!!!!

这是代码

#include <iostream>
#include <string>
using namespace std;
template<class T> void selectionSort(T arr[], T num)
{
    int pos_min;
    T temp;
    for (int i = 0; i < num - 1; i++)
    {
        pos_min = i;
        for (int j = i + 1; j < num; j++)
        {
            for (arr[j] < arr[pos_min])
            {
                pos_min = j;
            }
        }

        if (pos_min != i)
        {
            temp = arr[i];
            arr[i] = arr[pos_min];
            arr[pos_min] = temp;
        }
    }
}
int main()
{
    char check = 'C';
    while (toupper(check) != 'Q')
    {
        char dataType;
        int num = 0;
        cout << "What kind of data do you want to sort?" << endl;
        cout << " For integer enter i, for string enter s, for character enter c. ";
        cin >> dataType;
        //User input dataType
        if (toupper(dataType) == 'I')
        {
            int arr[100];
            int input;
            cout << " You've chosen Integer dataType" << endl;
        }
        else if (toupper(dataType) == 'S')
        {
            string arr[100];
            string input;
            cout << " You've chosen String dataType" << endl;
        }
        else if(toupper(dataType) == 'C')
        {
            char arr[100];
            char input;
            cout << " You've chosen Character dataType" << endl;
        }
        else
        {
            cout << "Not a recognizable dataType. Shuting down..." << endl;
            return -1;
        }
        //User input # of num
        cout << "How many num will be sorted? ";
        cin >> num;
        for (int i = 0; i < num; i++)
        {
            cout << "Enter an input of the dataType you selected: ";
            cin >> input;
            arr[i] = input;
        }
        //Display user input
        cout << "The data as you entered it: ";
        for (int i = 0; i < num; i++)
        {
            cout << arr[i];
            cout << " ";
        }
        cout << endl;
        //Sort user input by calling template functon selectionSort
        selectionSort(arr, num);
        //Display sorted user input
        cout << "After sorting your data by calling selectionSort: ";
        for (int i = 0; i < num; i++)
        {
            cout << arr[i];
            cout << " ";
        }
        cout << endl;
        //Query user to quit or continue
    cout << " Would you like to continue? Enter 'Q'. Enter anything else to continue.";
    cin >> check;
    }

    return 0;
}

这是因为您在if/else块中声明了它们。一旦块完成,这些变量就会超出范围,不再可访问
解决这一问题的一种方法是始终将输入作为字符数据读取,然后在事实发生后将其转换为指定的类型。请参阅atoi了解如何从char转换为int。

变量永远不能有未知类型。即使在模板内部,每个变量的类型对于任何特定的实例化都是固定的。

这提出了一个解决方案。处理具有多种类型的变量的所有代码都可以放在模板函数中。

您可能会发现传递任意元素类型的任意长度数组的模板语法非常有用:

template<typename T, size_t N>
void func1( T (&arr)[N] )
{
    //...
}

但你真的不需要通过数组。只需传递一个类型,并在函数内部创建数组时使用该类型。

template<typename T>
void process_it()
{
    T arr[100];
    T input;
    // now work on them
}

无论哪种方式,您都需要从所有if/else分支内部调用此函数,其中确切的类型是已知的。