Unfamiliar name lookup error

While attempting to create a function that would sort a defined number of random values, I had an error I was unfamiliar with (line notated by comment).

Error read as follows:
[Error] name lookup of 'n' changed for ISO 'for' scoping [-fpermissive]
Note read as follows
[Note] (if you use '-fpermissive' G++ will accept your code)

Great help if anyone can define this error, and how it may be resolved.
Thanks

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
// C++ File  --  Sorting various number of randomly generated values

#include <iostream>
#include <cstdlib>
#include <ctime>
using namespace std;

int main()
{
	int Nvals;
	
	cout<<"Number of values >> ";
	cin>>Nvals;
	
	int vals[Nvals];
	
	srand(time(0));
	/*
	for (int n = Nvals, inc = -1; n>0; n--, inc++); {
		vals[inc] = rand();
		cout<<"Value "<<inc<<" is "<<vals[inc]<<"\n";
	}
	*/
	for (int n = 0; n != Nvals; n++); {
		vals[n] = rand();             //Error is here
		cout<<"Value "<<n<<" is "<<vals[n]<<"\n";
	}
	//Sorting	
}

The for loop on line 24 finishes at that semi-colon. The semi-colon on line 24.

While I'm here, this:
int vals[Nvals];
is illegal C++ and your compiler shouldn't let you do it without at least telling you that it's illegal.
Thank you for the syntax update! Issue was resolved.

Also, why is:
int vals[Nvals];
bad initialization? The g++ compiler ran the code as expected it seems.
Last edited on
> The g++ compiler ran the code as expected it seems

By default, the GNU compiler does not conform to standard C++.

We need to explicitly turn on standard conformance with the options -std=c++14 -pedantic-errors
(yes, both are required).

1
2
3
4
5
6
7
8
9
10
11
12
#include <iostream>

int main()
{
	int Nvals;
	
	std::cout << "Number of values >> ";
	std::cin >> Nvals;
	
	int vals[Nvals]; // error: ISO C++ forbids variable length array 'vals'
	                 // error: variable length arrays are a C99 feature
}

http://coliru.stacked-crooked.com/a/e075d031aac0d23f
http://rextester.com/GUS32156
Ah, gotcha. That's unfortunate. I'll make the changes soon.

Sense I've not dealt with this before, is there a nifty way I can make an array of dynamic length?
> is there a nifty way I can make an array of dynamic length?

Yes. Use std::vector<> https://cal-linux.com/tutorials/vectors.html
Very nifty.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
#include <iostream>
#include <cstdlib>
#include <ctime>
#include <vector>

int main() {

    // consider using facilities in <random>
    // http://en.cppreference.com/w/cpp/numeric/random
    std::srand( std::time(nullptr) ) ;

    std::size_t Nvals ; // http://en.cppreference.com/w/cpp/types/size_t
    std::cout << "Number of values >> " ;
    std::cin >> Nvals;

    std::vector<int> vals(Nvals) ; // http://en.cppreference.com/w/cpp/container/vector

    // fill with random values in the range 0 to 99
    // http://www.stroustrup.com/C++11FAQ.html#for
    for( int& v : vals ) v = std::rand()%100 ;

    for( std::size_t n = 0 ; n < vals.size() ; ++n )
        std::cout << "vals[" << n << "] == " << vals[n] << '\n' ;

    // ...
}

http://coliru.stacked-crooked.com/a/e02e20652b47099a
Last edited on
Oh, nifty indeed. I'll be sure to incorporate std::vector into the program.

Thanks again!
Last edited on
Registered users can post here. Sign in or register to post.