1
#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]);
}

This is the code I have, it's not very good example, but I quickly wrote it to represent my problem I have because my project is kinda too big. So basically, I want to call struct from a different function and I'm getting this error: error: no match for 'operator[]' in guy[i] on this line : check_ID(people, guy[i]); in the function check_something.

3
  • what's the purpose of guy[i]? if you want to be able to apply subscript operator to struct workers, of course you need to define it, the compiler's making it very clear. Commented Jan 14, 2014 at 3:27
  • What do you want/expect check_something to do? Commented Jan 14, 2014 at 3:33
  • My fault.. I forgot void returns nothing, it seems to work now! :-) Thanks a lot for your help. Commented Jan 14, 2014 at 3:35

3 Answers 3

5

In main, people is an array. You access the ith element of it people[i] and try to pass it to check_something in the position of function-local variable guy. You then try to dereference guy - which is not an array, but a single object instance.

int main()
{
    workers people[5];  // <-- array

...

    check_something(people /* <-- people */, people[i] /* <-- guy */, i /* <-- i */);

vs

void check_something(workers *people, workers &guy, int& i){
    check_ID(people, guy[i] /* <-- array access on single instance*/);

You actually passed the array in the first argument, people. You don't need "guy" here, because it is people[i], isn't it? So you could do:

void check_something(workers *people, int& i){
    worker& guy = people[i];
    check_ID(people, guy);

or just

void check_something(workers *people, int& i){
    check_ID(people, people[i]);

or

would work, or you could just pass

void check_something(workers* people, workers& guy) {
    check_id(people, guy);
}

---- EDIT ----

You also have a python-like bug in your check_ID function.

   if(people[i].ID == guy.ID)
        return true;
        break;

In Python, this says:

if people[i].ID == guy.ID:
    return True

break

What you want is

if ( people[i].ID == guy.ID ) {
    return true;
    break;
}

or just

if ( people[i].ID == guy.ID )
    return true;

(since the return is going to exit the function, there's no point in also saying break afterwards)

Sign up to request clarification or add additional context in comments.

5 Comments

I declared workers guy = people[i]; and when I call the function with check_ID(people, guy), It still does nothing (doesn't do its function). Why is that? I also tried people[i] as second reference and it still doesn't output anything.
workers guy = people[i] makes a copy of people[i] into a temporary, local variable called guy.
Also, your 'check_ID' function is broken because you have a 'break' that always executes.
Updated my answer to include the check_ID/return/break thing.
Thanks a lot for the help! I really should start using {} everywhere, even if it's one line code I guess.
0

workers does not have an overloaded subscript operator, nor is guy an array. Therefore you cannot call [] on it. Either create an array or delete [i] after guy.

check_ID(people, guy); //delete [i]

1 Comment

I did try with guy, but it does nothing.. it goes through the function, does the call to check_ID, but the check_ID does nothing, I guess because the guy has no value or something?
0

Guy is a reference, not a pointer. You cannot use the operator[] on a reference.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.