Why doesn't my code work?


#1

i keep getting a !no visible @interface for ‘BNRForeignStockHolding’ declares the selector ‘…’

here is my code for BNRStockHolding.h

#import <Foundation/Foundation.h>

@interface BNRStockHolding : NSObject

@property (nonatomic) NSString *name;
@property (nonatomic) float purchaseSharePrice;
@property (nonatomic) float currentSharePrice;
@property (nonatomic) int numberOfShares;

  • (float)costInDollars;
  • (float)valueInDollars;

@end

here is my code for BNRStockHolding.m

#import “BNRStockHolding.h”

@implementation BNRStockHolding

@synthesize name;

  • (float)costInDollars
    {
    return _purchaseSharePrice * _numberOfShares;
    }
  • (float)valueInDollars
    {
    return _currentSharePrice * _numberOfShares;
    }

@end

here is my code for BNRForeignStockHolding.h

#import <Foundation/Foundation.h>
#import “BNRStockHolding.h”

@interface BNRForeignStockHolding : BNRStockHolding

@property (nonatomic) NSString *name;
@property (nonatomic) float conversionRate;

@end

here is my code for BNRForeignStockHolding.m

#import “BNRForeignStockHolding.h”

@implementation BNRForeignStockHolding

@synthesize name;

  • (float)costInDollars
    {
    return _purchaseSharePrice * _conversionRate * _numberOfShares;
    }

  • (float)valueInDollars
    {
    return _currentSharePrice * _conversionRate * _numberOfShares;
    }

@end

and finally code for main.m

#import <Foundation/Foundation.h>
#import “BNRStockHolding.h”
#import “BNRForeignStockHolding.h”

int main(int argc, const char * argv[])
{
@autoreleasepool {

    BNRStockHolding *apple = [[BNRStockHolding alloc] init];
    BNRStockHolding *microsoft = [[BNRStockHolding alloc] init];
    
    BNRForeignStockHolding *linux = [[BNRForeignStockHolding alloc] init];
    
    [apple setName:@"Apple"];
    [apple setPurchaseSharePrice:2.3];
    [apple setCurrentSharePrice:4.5];
    [apple setNumberOfShares:40];
    
    [microsoft setName:@"Microsoft"];
    [microsoft setPurchaseSharePrice:12.19];
    [microsoft setCurrentSharePrice:10.56];
    [microsoft setNumberOfShares:90];
    
    [linux setName:@"Linux"];
    [linux setPurchaseSharePrice:45.1];
    [linux setCurrentSharePrice:49.51];
    [linux setNumberOfShares:210];
    [linux setConversionRate:0.94];

    NSArray *stocks = @[apple, microsoft, linux];
    
    for (BNRStockHolding *d in stocks)
    {
        NSLog(@"%@ cost is %.2f and value is %.2f", d.name, d.costInDollars, d.valueInDollars);
    }
    }
return 0;

}

the code worked perfect for the previous challenge, but once i added the subclass in i got a bunch of errors. i followed it exactly like the example from the book so i don’t know why it’s not working. any and all help really appreciated!


#2

[quote]i keep getting a !no visible @interface for ‘BNRForeignStockHolding’ declares the selector ‘…’
[/quote]
Which selector is that? You probably forgot to include something.


#3

Hey… Looks like we’re along the same chapter. I’d be happy to continue working through the book with you if you’d like, Now, I haven’t tested it, but take a look at your foreignStockHolding.h file. You have the property for your conversion rate, but you’ve forgotten to actually add the instance variable. It should look something like:

#import <Foundation/Foundation.h>
#import "StockHolding.h"

@interface ForeignStockHolding : StockHolding

{
    float conversionRate;
}

...


@end

In any case, if you’re interested in working along with me, send me a message, and we’ll figure it out.

edit:
You have to remember that your properties just replace the accessor methods but don’t actually instantiate the variables


#4

in BNRForeignStockHolding.m i got two errors each on these 2 lines

return _purchaseSharePrice * _conversionRate * _numberOfShares;

 // error --->  ! use of undeclared identifier '_purchaseSharePrice'
 // error ---> ! use of undeclared identifier -_numberOfShares'

return _currentSharePrice * _conversionRate * _numberOfShares;

 // error --->! use of undeclared identifier '_currentSharePrice' 
 // error --->! use of undeclared identifier -_numberOfShares'

edited* i changed these lines to return self.purchaseSharePrice * self.conversionRate…etc. and those 4 errors went away. i am still stuck with the 5 errors below though.

then in main.m i got 5 errors - one for each line when setting values for the BNRForeignStockHolding class

    [linux setName:@"Linux"];                  //error ---> !no visible @interface for 'BNRForeignStockHolding' declares the selector 'setName;'
    [linux setPurchaseSharePrice:45.1];    //error ---> !no visible @interface for 'BNRForeignStockHolding' declares the selector 'setPurchaseSharePrice;'
    [linux setCurrentSharePrice:49.51];    //error ---> !no visible @interface for 'BNRForeignStockHolding' declares the selector 'setCurrentSharePrice;'
    [linux setNumberOfShares:210];         //error ---> !no visible @interface for 'BNRForeignStockHolding' declares the selector 'setNumberOfShares;'
    [linux setConversionRate:0.94];         //error ---> !no visible @interface for 'BNRForeignStockHolding' declares the selector 'setConversionRate;'

in the first project in this chapter, when i created a subclass, i didn’t need to declare the selectors because they inherited the info from the superclass. these errors seem like my problem is that i didn’t import the superclass into my BNRForeignStockHolding class, but clearly i did. so i still have no clue what i am doing wrong here.


#5

the code is perfectly fine. i found my problem. i had accidentally created a subclass at first but it got stored in the wrong spot so i tried to delete it, but i guess it was still linked to the project. so i went into the folders and found it, deleted it, and now the program succeeds. thank you for your help though!

looneylu - i am new to this forum so i wasn’t able to find how to message you. on your page under contact info it was blank so maybe i was in the wrong spot. feel free to message me if you do want to study together