mardi 6 janvier 2015

I'm preparing to write a C++ application for OS X with a Cocoa front end. To get started, I've been watching videos on Cocoa / Swift / Interface Builder. I found a tutorial here making a simple tip calculating app. My thoughts were to start small and convert this example application from using a Swift model to a C++ model.


However, I'm running into an issue with the Swift compiler not finding the unordered_map include file. It doesn't seem to have any issue with the vector include and so my first instinct was that the C++11 compiler option was not set. It turns out it is, and I'm still not certain why I get this. Do my C++ files need to be compiled separately with the GCC or Clang compilers?


C++ .h:



#ifndef __TipCalculator__TipCalcModel__
#define __TipCalculator__TipCalcModel__

#include <stdio.h>

#include <unordered_map>
#include <vector>

using std::unordered_map;
using std::vector;



class TipCalcModel
{

private:

double _total;
double _taxPct;
double _subtotal;


public:

TipCalcModel(double total, double taxPct) : _total(total), _taxPct(taxPct) {};



unordered_map<int, double>* returnPossibleTips();

inline double getSubtotal() { return _total / (_taxPct + 1); }

inline double calcTipWithTipPct(double tipPct) { return _subtotal * tipPct; }

};

#endif /* defined(__TipCalculator__TipCalcModel__) */


C++ .cpp:



#include "TipCalcModel.h"

#include <unordered_map>
#include <vector>

using std::unordered_map;
using std::vector;



unordered_map<int, double>* TipCalcModel::returnPossibleTips()
{
unordered_map<int, double>* possTipMap = new unordered_map<int, double>;

vector<double> possPct;
possPct.push_back(0.15);
possPct.push_back(0.18);
possPct.push_back(0.20);

for (vector<double>::const_iterator pctIter = possPct.cbegin(); pctIter != possPct.cend(); ++pctIter)
{
possTipMap->insert(std::make_pair(*pctIter * 100, calcTipWithTipPct(*pctIter)));
}


return possTipMap;

}


My attempt at a Wrapper for the C++ code.


.h:



#ifndef TipCalculator_TipCalcModelWrapper_h
#define TipCalculator_TipCalcModelWrapper_h


#import <Foundation/Foundation.h>
#include "TipCalcModel.h"


TipCalcModel *tipCalcModel;

@interface CPlusPlusTipCalc : NSObject

- (void) dealloc;

+ (NSString*) returnPossibleTips;

+ (double) getSubtotal;

+ (double) calcTipWithTipPct : (double) tipPct;

@end

#endif


.mm:



#import <Foundation/Foundation.h>

#include "TipCalcModelWrapper.h"
#include "TipCalcModel.h"



@implementation CPlusPlusTipCalc
- (id)init {
if(self = [super init]) {
tipCalcModel = new TipCalcModel(0, 0.0);
}
return self;
}
- (void) dealloc {
if(tipCalcModel != NULL) delete tipCalcModel;
}

+ (NSString*) returnPossibleTips {

unordered_map<int, double>* map = tipCalcModel->returnPossibleTips();


unordered_map<int, double>::const_iterator mapKey = map->cbegin();

NSString* retString;


while (mapKey != map->cend())
{
NSString* intermediateString = [NSString stringWithFormat:@"%d %f\n", mapKey- >first, mapKey->second];

retString = [retString stringByAppendingString:intermediateString];

++mapKey;
}

return retString;
}

+ (double) getSubtotal {
return tipCalcModel->getSubtotal();
}

+ (double) calcTipWithTipPct : (double) tipPct {
return tipCalcModel->calcTipWithTipPct(tipPct);
}


@end


My header bridge file only contained the following (and now am not sure if this is correct, either):



#include "TipCalcModelWrapper.h"


There could be numerous places I'm going wrong here. Any help and explanation is greatly appreciated. If you have any good resources for hybrid C++ and Cocoa - please post!!


I'm using Xcode 6.1.1.


Thank you for your time!


Aucun commentaire:

Enregistrer un commentaire