Hi,

I dont know much about programming, however I have a c++ program(i didnt write it) that I need to use it to do some experiments. When I compile the program I get this errror.

dynn.cpp: In function ‘void make_near()’:
dynn.cpp:374: error: name lookup of ‘j’ changed for new ISO ‘for’ scoping
dynn.cpp:370: error:   using obsolete binding at ‘j’

Could you help me to fix this please? As I mentioned before i dont know much about programming.So I would appreciate it if you can guide me in the simplest way as possible. Thank you.

dynn.cpp

void make_near()
{
	int real_input;
	int real_hidden;
	
	real_input = xnow.m_input + xnow.m_input*(xnow.m_input+1)/2;
	real_hidden = 2 * real_input;	
	int alter = (real_input + 2) * real_hidden + 1;
	int length1 = (real_input + 1) * real_hidden;
	int length2 = real_hidden + 1;
	alter = alter / 10 + 1;
	int p;
	int p1;
	int p2;
    for(int i=0;i<NO_OF_POPU;i++)
	{
		for(int j=0;j<(real_input + 1) * real_hidden;j++)
		{
			xnear[i].m_weights1[j] = xnow.m_weights1[j];
		}
		for(j=0;j<real_hidden+1;j++)
		{
			xnear[i].m_weights2[j] = xnow.m_weights2[j];
		}
		for(int k=0;k<alter;k++)
		{
            p = (int)(rand() % 2);
			if(p == 0)
			{
				p1 = (int)(rand() % length1);
				xnear[i].m_weights1[p1] += produce_rand_double(-1./LENGTH,1./LENGTH);
			}
			if(p == 1)
			{
				p2 = (int)(rand() % length2);
				xnear[i].m_weights2[p2] += produce_rand_double(-1./LENGTH,1./LENGTH);
			}
		}
		xnear[i].m_fitness = count_nn_fitness(xnear[i]);
	}
}

Dani AI

Generated

A short, practical explanation and a couple of safe fixes.

The compiler message "name lookup of 'j' changed for new ISO 'for' scoping" means the C++ standard makes a variable declared in a for-loop init visible only inside that for statement. Code that relied on older compiler behavior (where the loop variable still existed after the loop) will fail to compile under a standards-conforming compiler. (cppreference.com)

What happened in this thread is exactly the common case: a loop declared j inside its header, then later code tries to reuse j outside that loop. correctly pointed out two straightforward fixes: either give j a declaration in the outer block (with the other int declarations) so later for statements can use it, or declare j freshly in each loop header so its lifetime is only the loop. Either fix is valid; choosing which one depends on whether you actually need j after the loop finishes. (stackoverflow.com)

Two extra tips that commonly save hours:

  • Watch for a stray semicolon after a for(...) line (for(...); ) — that makes the loop body an empty statement and the following block run once with j out of scope.
  • Prefer the tighter scope where possible (declare the counter in the for header) — it reduces mistakes and is the modern C++ style. If you are porting old code, resist the temptation to silence the compiler by changing flags; fix the declarations instead. If you do want stricter diagnostics during development, enable -Wall -Wextra (and optionally -Werror) to catch these kinds of issues early. (stackoverflow.com)

Summary: 's advice was correct and the error is a standards scoping issue; either redeclare j where you need it or move a single int j; into the outer scope. This is the clean, portable fix. (cppreference.com)

Recommended Answers

All 2 Replies

At line 17 of the code you posted the variable j is only visible in that for loop. If you want to use that variable again on line 21 it will have to be declared again just like it was on line 17. Another way to fix the problem is to declare that variable on line 14 similar to the way those other integers were declared, then remove the declaration on line 17 to make line 17 similar to line 21.

Thank you very much, problem is fixed.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.