Challenge runs, but I get 'incompatible pointer types'


#1

This is the code.

portfolio.h

[code]#import <Foundation/Foundation.h>
@class Stockholding;

@interface Portfolio : NSObject
{
NSMutableArray *portfolio;
}

  • (void)addPortfolioObject:(Stockholding *)stock;
  • (float)valueOfPortfolio;
  • (int)numberOfStocksInPortfolio;

@end
[/code]

portfolio.m

[code]#import “Portfolio.h”
#import “StockHolding.h”

@implementation Portfolio

  • (void)addPortfolioObject:(Stockholding *)stock
    {
    // Har vi allerede et array, eller skal vi lave et nyt?
    if (!portfolio) {
    portfolio = [[NSMutableArray alloc] init];
    }
    [portfolio addObject:stock];
    }

  • (float)valueOfPortfolio
    {
    float totalValue = 0.0;
    for (StockHolding *s in portfolio) {
    totalValue += [s valueInDollars];
    }
    return totalValue;
    }

  • (int)numberOfStocksInPortfolio
    {
    // Løb igennem porteføljen og tæl antallet af aktioer
    int totalStocks = 0;
    for (StockHolding *s in portfolio) {
    totalStocks += [s numberOfStocks];
    }
    return totalStocks;
    }

@end[/code]

Stockholding.h

[code]#import <Foundation/Foundation.h>

@interface StockHolding : NSObject
{
float purchasePrice;
float currentPrice;
int numberOfStocks;
}

@property float purchasePrice, currentPrice;
@property int numberOfStocks;

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

@end[/code]

Stockholding.m

[code]#import “StockHolding.h”

@implementation StockHolding

@synthesize purchasePrice, currentPrice, numberOfStocks;

  • (float)costInDollars
    {
    return [self numberOfStocks] * [self purchasePrice];
    }

  • (float)valueInDollars
    {
    return [self numberOfStocks] * [self currentPrice];
    }

@end[/code]

and finally main.m

[code]#import <Foundation/Foundation.h>
#import “StockHolding.h”
#import “Portfolio.h”

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

@autoreleasepool {
    StockHolding *stock1 = [[StockHolding alloc] init];
    [stock1 setPurchasePrice:40.0];
    [stock1 setCurrentPrice:38.7];
    [stock1 setNumberOfStocks:30];
    
    StockHolding *stock2 = [[StockHolding alloc] init];
    [stock2 setPurchasePrice:13.0];
    [stock2 setCurrentPrice:78.2];
    [stock2 setNumberOfStocks:12];
    
    Portfolio *portfolio = [[Portfolio alloc] init];
    [portfolio addPortfolioObject:stock1];
    [portfolio addPortfolioObject:stock2];
    
    [portfolio valueOfPortfolio];
    
    NSLog(@"Portfolio value %.2f", [portfolio valueOfPortfolio]);
    
    NSLog(@"Stocks in portfolio %d", [portfolio numberOfStocksInPortfolio]);
} 
return 0;

}[/code]

It compiles and runs, and gives the correct result. But an error for these two lines

[portfolio addPortfolioObject:stock1];
[portfolio addPortfolioObject:stock2];

The error is Incompatible pointer types sending ‘Stockholding *__strong’ to parameter of type ‘Stockholding *’

What is wrong?


Lars


#2

Watch your cases: StockHolding vs. Stockholding - you incorrectly reference Stockholding several times, especially in the Portfolio class.

Make sure to import StockHolding into your Portfolio class, and that all objects are correctly referenced as type “StockHolding” elsewhere - C is case sensitive. Remove the class declaration of “Stockholding” in your Portfolio class, since that’s obscuring your mistaken use of the non-existent Stockholding class.


#3

Thank you! Sometimes you get blind looking at your own writing. You know what it’s supposed to say and ignore what it actually says. Having a second pair of eyes look it over is invaluable.

Lars