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
// C++ File -- Sorting various number of randomly generated values
#include <iostream>
#include <cstdlib>
#include <ctime>
usingnamespace 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 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
}