Thread: What is this error I am getting?

  1. #1
    Registered User Welder's Avatar
    Join Date
    Oct 2007
    Location
    Washington
    Posts
    100

    What is this error I am getting?

    Sorry, I posted this in the C# forum first but I meant to post it here.

    What is this error I am getting and does anybody know how I can fix it? I don't do C++, only C but am using some image displaying code I found on the internet that was written in C++.

    "name lookup of `n' changed for new ISO `for' scoping "

    Code:
    void C_ImageSet::AddImage (C_Image* newimage)
    {
    	C_Image ** pTempImg = new C_Image* [nImages+1];
    	for (int n=0;n<nImages;n++) pTempImg[n]=img[n];	// (pointer copy)
    	delete[] img;
    	img=pTempImg;
    	img[n]=newimage;
    	nImages++;
    }

  2. #2
    erstwhile
    Join Date
    Jan 2002
    Posts
    2,227
    The variable n has scope limited to the for loop ( a single line in your example). Declare n with function scope ie before the for loop and you should be good to go.
    CProgramming FAQ
    Caution: this person may be a carrier of the misinformation virus.

  3. #3
    Registered User
    Join Date
    May 2006
    Posts
    903
    n is used outside of the for loop in which it was declared. Therefore, it cannot be utilized in that line. Declare n before your for loop.

    Edit: Beaten.

  4. #4
    Registered User Welder's Avatar
    Join Date
    Oct 2007
    Location
    Washington
    Posts
    100
    Oh sweet thanks.

  5. #5
    Registered User
    Join Date
    Apr 2006
    Posts
    2,149
    That would work, but n will always be the same as nImages after the loop finishes, so you might as well us nImages instead of n there. Assuming that's what you want, which may not be the case.
    It is too clear and so it is hard to see.
    A dunce once searched for fire with a lighted lantern.
    Had he known what fire was,
    He could have cooked his rice much sooner.

  6. #6
    Registered User Welder's Avatar
    Join Date
    Oct 2007
    Location
    Washington
    Posts
    100
    I got it working, Thanks King Mir and everybody else who contributed.

Popular pages Recent additions subscribe to a feed