错误 C2109:下标需要数组或指针类型 5
error c2109: subscript requires array or pointer type 5
我一直遇到一个关于for循环和数组的小问题,我希望我能得到一些帮助。在第一个函数中,使用 for 循环调用函数的每个值就可以了。但是,在第二个函数中,我从Visual Studio收到一个错误,指出"下标需要数组或指针类型"。我在这里做错了什么导致此错误?
该程序的目的是搜索书籍的txt文件,跳过条目之间的行,找出文件中有多少条目匹配以及它们的位置,并打印出每个条目的详细信息。
void bookSearch(string id) {
ifstream fbooks;
string item = " ", entry = " ";
int resultLocation[30];
int searchType = 0;
fbooks.open("books.txt");
cout << "Welcome to the Book Search System, " << id << ".n"
<< "What do you wish to search the registry by?n"
<< "1. ISBN Numbern" << "2. Authorn" << "3. Titlen";
while (searchType<1 || searchType>3) {
cin >> searchType;
if (searchType<1 || searchType>3) {
displayMessage(0);
}
}
getline(cin, item);
for (int x = 0; x <= 30; x++) {
for (int y = 0; y < searchType; y++)
getline(fbooks, entry);
if (entry == item)
resultLocation[x] = 1;
else
resultLocation[x] = 0;
for (int z = 0; z < 5; z++)
getline(fbooks, entry);
}
resultPrint(resultLocation, id);
}
void resultPrint(int resultLocation, string id){
int resultNum = 0;
string entry = "";
ifstream fbooks;
fbooks.open("books.txt");
for (int a = 0; a <= 30; a++) {
if (resultLocation == 1)
resultNum++;
}
if (resultNum > 0) {
cout << endl << "There are " << resultNum << " entries in the database matching that criteria.n";
for (int a = 0; a <= 30; a++){
if (resultLocation[a] == 1) { //The a in this line is marked with the error
for (int b = 0; b <= 2; b++) {
getline(fbooks, entry);
cout << entry;
}
}
}
}
else
cout << endl << "There are no entries in the database matching that criteria.n";
}
resultLocation
是一个整数,所以你不能对它使用operator[]
(除了第一个"索引")。看起来你的意思是把它变成一个数组:
void resultPrint(int resultLocation[], string id);
相关文章:
- C++中的双指针类型转换
- C++默认情况下,指针类型数组的元素是否保证初始化为 nullptr?
- 将类指针类型转换为键时出错
- 错误:表达式必须具有算术、无作用域枚举或带有运算符重载的指针类型
- C++在一个映射中存储不同的指针类型(并处理销毁)
- 指针类型类成员的动态强制转换的恒定性是什么?
- 我正在尝试将表的地址传递给要在另一个函数中使用的指针,但得到不兼容的指针类型
- 在将派生类指针类型转换为派生类指针后,从基类指针调用派生类函数
- 如何使用静态多态性在 int 和指针类型之间进行转换?
- STL 函数和函数类型与函数指针类型
- 如何调用指针类型的方法(禁用多态性)?
- 为什么新表达式可以正确生成指针类型,即使它应该返回 void*?
- 对于非常量指针类型的参数,未调用具有常量指针模板类型参数的功能
- 是否允许调用方对我的 Builder 类使用任何指针类型(包括智能指针)?
- OPENCL 警告:不兼容的指针类型将'float __global[16]'传递给类型为 '__global float4 的参数 *
- 专门用于"direct"函数类型(与函数指针类型相对)
- 指向成员的指针类型和模板
- 返回对常量结构(指针类型)成员的引用:明显的左值到右值转换
- 在C++17中,为什么类模板和函数模板的指针类型推导明显不一致
- 为什么允许将整型、枚举和指向成员的指针类型reinterpret_cast到自身?