1
bool linear_search(const string A[], int n, string colour, int &count)
{
    for (int i = 0; i < n; i++);
    {
        if (colour == A[i])
        {
            return true;
        }
    }
    return false;
}

Compiling the above code results in the error 'i' was not declared in this scope for the if statement if (colour == A[i]).

This is really similar to many other for loops I have written, and I don't understand why it is not declared in the scope. Wasn't it declared in the previous line? How do I fix this?

3
  • 1
    you have ; after for loop Commented Sep 20, 2015 at 20:49
  • Consider using std::find Commented Sep 20, 2015 at 20:53
  • Can't believe I am "stack-overflowing" for this petty mistake. Commented Jul 19, 2017 at 17:12

5 Answers 5

11

You have a semi colon after your for loop declaration, remove it and you will be fine.

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

Comments

5

As others have pointed out, the problem is an extra semicolon that prevents your intended loop body from actually being part of the loop. But I want to provide more information on how to catch and avoid this kind of error.

First of all, when I compile the code with the formatting you show, my compiler produces a warning:

main.cpp:130:32: warning: for loop has empty body [-Wempty-body]
    for (int i = 0; i < n; i++);
                               ^

You should check to see if you're already getting this or some similar warning, and if so you should make sure to pay attention to warnings in the future. If you're not getting this warning, see if you can increase your compiler's warning level in some way to cause it to produce a warning like this. Enabling and paying attention to compiler warnings can save you a lot of trouble.

Next, I notice that your code is poorly formatted. Poor formatting can hide this sort of error. When I auto-format the code it becomes:

bool linear_search(const string A[], int n, string colour, int &count) {
  for (int i = 0; i < n; i++)
    ;
  {
    if (colour == A[i]) {
      return true;
    }
  }
  return false;
}

This formatting makes the extraneous semicolon much more obvious. (It also suppresses my compiler's warning about the empty body, since the compiler assumes that if you put the empty body on a separate line then you really mean for it to be empty.) Using automatic formatting avoids the problems of inconsistent formatting and ensures that the formatting is consistent with the actual meaning of the code. See if your editor provides formatting support or see if you can integrate an external formatter like clang-format.

Comments

2

Are you sure you need ; in this line? for (int i = 0; i < n; i++);

Comments

1

you ended for loops block by adding a ; after for loop

for (int i = 0; i < n; i++);

remove this semicolon.

Comments

0

I had a similar problem but there was an "if" statement before my variable declaration in front of a "for" loop, the error was the same. Just in case that somebody googled it and didn't mention something like that:

if (someVar!=1) // an "if" statement that reduces the scope of "var"
//some comment line
//some other comment line
double var = 0.0;

for (size_t i = 0; i < length; i++) {
    var *= 0.5; // Error appeared here
}

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.