简体   繁体   中英

“not in this scope” error in cpp function calling

I was trying to implement mergesort in cpp. However, the Dev-cpp 5.6.1 reports some "not in this scope" error. It said, "lo", "mid", and "hi" was not declared in this scope. Can you tell me why this happens? Thanks.

#include<iostream>
using namespace std;
void merge(int nums[], int lo, int mid, int hi) {
    int n1 = mid - lo + 1;
    int n2 = hi - mid + 1;
    /*Other implementation code omitted*/
}
int main() {
    /*code to Input numbers into nums[]*/
    merge(nums, 0, 4, 9);
    return 0;
}

When reading error messages that involve syntax errors, it is important to pay the most attention to the very first one. This is because the C++ compiler is not obligated to stop at the first error, and more errors may be produced as a consequence of the first.

In your original code, you have a syntax error in your declaration of the nums parameter to your merge() function:

#include<iostream>
using namespace std;
int nums[10];
void merge(int[] &nums, int lo, int mid, int hi) {
    int n1 = mid - lo + 1;
    int n2 = hi - mid + 1;
    /*Other implementation code omitted*/
}
int main() {
    /*code to Input numbers into nums[]*/
    merge(nums, 0, 4, 9);
}

You have since edited your program to remove that syntax error, but with this original code, I see the following error messages:

x.cc:3: error: expected ',' or '...' before '&' token
x.cc: In function 'void merge(int*)':
x.cc:4: error: 'mid' was not declared in this scope
x.cc:4: error: 'lo' was not declared in this scope
x.cc:5: error: 'hi' was not declared in this scope
x.cc: In function 'int main()':
x.cc:3: error: too many arguments to function 'void merge(int*)'
x.cc:11: error: at this point in file

You reported the was not declared in this scope errors, but failed to mention the first one, which is pointing out the syntax error for nums . Because of this syntax error, the compiler failed to parse the rest of the function parameters, so it reported those identifiers as having no visible declaration.

Fixing this error removes the others. This is because the parser for the function prototype now picks up the rest of the function arguments, so those are now in scope for the rest of the function.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM