稍后在程序中使用if-else构造内部声明的变量会导致未声明的标识符错误
Using variables declared inside an if else construct later in the program results in an undeclared identifier error
这个程序非常不言自明,所以我不会真正讨论它的用途。
我现在的主要问题是在第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
分支内部调用此函数,其中确切的类型是已知的。
相关文章:
- 在将变量声明为引用时,堆在释放后使用
- 静态变量声明和定义
- 在变量声明中使用 for 循环
- 向量索引变量声明(size_t 或 std::vector<DATATYPE>::size_type)
- C++ 类型类的变量声明不命名类型?
- 将本地 OpenCV Mat 变量声明为静态以防止重新分配
- std::enable_if 更改成员 *变量* 声明/类型
- 在C++中,变量声明左侧的大括号是什么意思?
- 将变量声明为全局变量,然后声明为局部变量 -Shadowing-
- 命名空间内C++变量声明
- 将静态全局变量声明为内联有什么意义吗?
- 如何将一个变量声明为另一个变量的值
- 如何将迭代器变量声明为私有成员变量
- 没有变量声明为函数,但错误:二进制表达式的操作数无效
- 有没有人有书面证据来保证函数中参数的定义方式与普通变量声明相同?
- ClangTool 如何获取变量声明中模板参数的位置?
- 标准与显式自动推导变量声明
- C 静态变量声明怪异链接器错误
- 如何为数组成员变量声明 getter/setter
- 可视C++变量声明