Code won't compile

Discussion in 'Mac Programming' started by aaron5466, Aug 11, 2011.

  1. aaron5466, Aug 11, 2011
    Last edited by a moderator: Aug 12, 2011

    aaron5466 macrumors newbie

    Joined:
    Aug 11, 2011
    #1
    I'm trying to write a program to draw a line segment. Right now, I've written it to draw three asterisks. It won't compile. Here it is:
    Code:
    //
    //  SegmentView.m
    //  segment
    //
    //  Created by Aaron Poley on 1/21/11.
    //  Copyright 2011 __MyCompanyName__. All rights reserved.
    //
    
    #import "SegmentView.h"
    
    int start_x = 15; //starting x coordinate of segment
    int start_y = 75; // starting y coordinate of segment
    int n = 0;
    
    @implementation SegmentView
    
    - (id)initWithFrame:(NSRect)frame {
        self = [super initWithFrame:frame];
        if (self) {
            // Initialization code here.
        }
        return self;
    }
    
    - (void)drawRect:(NSRect)dirtyRect {
        // Drawing code here.
    	//while loop below seems to cause an infinite loop.  for loop has same problem
    	n = 0;	// for while loop.  NSPoint might create an immutable point, whatever that means.  
    	while (n<2){
    
    		NSString* hello = @"*";
    		NSPoint point = NSMakePoint(n + start_x,n + start_y);
    		n = n+1;
    	}
    		NSMutableDictionary* font_attributes = [NSMutableDictionary new];
    
    		NSFont* font = [NSFont fontWithName:@"Futura-MediumItalic" size:12];
    
    		[font_attributes setObject:font forKey:NSFontAttributeName];
    		
    		[hello drawAtPoint:point withAttributes:font_attributes];
    		
    		[font_attributes release];
    	//}
    }
    
    @end
    
    When I try to build it, I get an "Unused variable 'hello'" warning at line 31, an "Unused variable 'point'" warning at line 32, and "'hello' undeclared" and "'point' undeclared" failures at line 41. (Sorry that line numbers aren't showing. I couldn't figure out how to get them to show in XCode and thought trying to put them in myself might confuse things more.) I tried adding

    char point;
    char hello;

    under line 12, "int n = 0;" but that didn't compile either.

    The code will compile when I comment out the "while n<2 {" and the closing bracket of the while loop block. I tried a for loop instead of a while loop but had similar problems. Without a loop, this code is not very useful. The fact that it works without a loop thoroughly confuses me.

    I'm not very familiar with the XCode IDE, so maybe it's something I'm doing wrong outside of the code, but my hunch is it's the code. Excuse me if I'm not using the right terminology. I haven't conversed with other developers much since I took my one course in programming 12 years ago. Sorry about the misplaced smilies too. If you know how to turn them off, let me know.

    Thanks for your help! :D
     
  2. mfram macrumors 65816

    Joined:
    Jan 23, 2010
    Location:
    San Diego, CA USA
    #2
    Variables "point" and "hello" are declared within a block. That means they are only in scope within that block.
     

Share This Page