【发布时间】:2014-01-14 03:21:08
【问题描述】:
#include <iostream>
#include <cmath>
using namespace std;
struct workers{
int ID;
string name;
string lastname;
int date;
};
bool check_ID(workers *people, workers &guy);
void check_something(workers *people, workers &guy, int& i);
int main()
{
workers people[5];
for(int i = 0; i < 5; i++){
cin >> people[i].ID;
cin >> people[i].name;
cin >> people[i].lastname;
cin >> people[i].date;
if(check_ID(people, people[i]) == true)
cout << "True" << endl;
else
cout << "False" << endl;
check_something(people, people[i], i);
}
return 0;
}
bool check_ID(workers *people, workers &guy){
for(int i = 0; i < 5; i++){
if(people[i].ID == guy.ID)
return true;
break;
}
return false;
}
void check_something(workers *people, workers &guy, int& i){
check_ID(people, guy[i]);
}
这是我的代码,它不是很好的例子,但我很快写了它来代表我遇到的问题,因为我的项目有点太大了。所以基本上,我想从不同的函数调用 struct 并且我收到了这个错误:
error: no match for 'operator[]' in guy[i] 在这一行:
check_ID(people, guy[i]); 在函数 check_something 中。
【问题讨论】:
-
guy[i] 的目的是什么?如果你希望能够对
struct workers应用下标运算符,当然你需要定义它,编译器已经说得很清楚了。 -
你想要/期望
check_something做什么? -
我的错..我忘记了 void 什么也没返回,它现在似乎工作了! :-) 非常感谢您的帮助。
标签: c++ arrays function parameters struct