2011-11-11 61 views
0

我已經在這裏徘徊到游泳池的盡頭。我取得了一些很好的進展,但現在我只是在肆虐。我試圖使用iOS的這一模糊邏輯的lib:http://code.google.com/p/fuzzy-lite/在iOS中使用C++ fuzzylite lib和ObjC(模糊邏輯)

我得到了它的編譯 - 我所做的是雙方在.cpp添加&的.h文件到我的項目,改變了後綴我主視圖控制器爲「.mm」。我能夠從viewDidload(show below)中運行fuzzyLite test.h文件。它運行並顯示測試數據。

我需要做的是創建一個fuzzyLite的持久化實例,這樣我就可以在我的應用程序中使用它(例如,能夠解決它,然後清理應用程序卸載時)。

我周圍搜索,但沒有理解在ObjC項目中包含C++代碼的討論/示例。有人可以向我展示一種方式,我可以繼續這樣做 - 包裝fuzzyLite代碼,以便我可以調用函數並返回結果?謝謝!

編輯:我在這裏使用的詳細方法取得這一進展: http://robnapier.net/blog/wrapping-c-take-2-1-486

有一件事我是不明確的是內存清理。 dealloc函數清除了封裝的CPP實例的實例 - 但是CCP實例中分配的內存呢?看起來像我需要調用一個方法來釋放,在刪除實例之前。

例如:包裝的類有一些子類的實例變量 - 是我的清理功能足以正確管理內存?

void Bingo::cleanup(){ 

delete engine; 
engine = NULL; 
delete health; 
health = NULL; 
delete energy; 
energy = NULL; 

} 

-header用於從ObjC包裝紙包裝的CPP類

#include "fuzzylite/FuzzyLite.h" 

namespace fl { 

class Bingo { 
public: 
    FuzzyEngine* engine; 
    OutputLVar* health; 
    InputLVar* energy; 
    Bingo(); 
    void Fuzz(); 

    void setInput(float input); 

}; 
} 

- (void)dealloc 
{ 
delete _cpp; 
_cpp = NULL; 

[super dealloc]; 
} 

FuzzyLiteIOSViewController.mm

#include "FuzzyLiteIOSViewController.h" 
#include "FuzzyLite.h" 
#include "test.h" 
#include <limits> 
#include "fuzzylite/FunctionTerm.h" 

//stuff not shown 

- (void)viewDidLoad 
{ 
    [super viewDidLoad]; 

    fl::Test* test = new fl::Test(); 
    test->SimpleMamdani(); 

} 

test.h

#ifndef FL_TEST_H 
#define FL_TEST_H 

namespace fl { 

class Test { 
public: 
    static void SimpleMamdani(); 

}; 
} 


#endif /* FL_TEST_H */ 

TEST.CPP

#include "fuzzylite/test.h" 
#include "fuzzylite/FuzzyLite.h" 
#include <limits> 

#include "fuzzylite/FunctionTerm.h" 
namespace fl { 

void Test::SimpleMamdani() { 
    FuzzyOperator& op = FuzzyOperator::DefaultFuzzyOperator(); 
    FuzzyEngine engine("simple-mamdani", op); 
    engine.hedgeSet().add(new fl::HedgeNot); 
    engine.hedgeSet().add(new fl::HedgeSomewhat); 
    engine.hedgeSet().add(new fl::HedgeVery); 
    fl::InputLVar* energy = new fl::InputLVar("Energy"); 
    energy->addTerm(new fl::ShoulderTerm("LOW", 0.25, 0.5, true)); 
    energy->addTerm(new fl::TriangularTerm("MEDIUM", 0.25, 0.75)); 
    energy->addTerm(new fl::ShoulderTerm("HIGH", 0.50, 0.75, false)); 
    engine.addInputLVar(energy); 

    fl::OutputLVar* health = new fl::OutputLVar("Health"); 
    health->addTerm(new fl::TriangularTerm("BAD", 0.0, 0.50)); 
    health->addTerm(new fl::TriangularTerm("REGULAR", 0.25, 0.75)); 
    health->addTerm(new fl::TriangularTerm("GOOD", 0.50, 1.00)); 
    engine.addOutputLVar(health); 
    fl::RuleBlock* block = new fl::RuleBlock(); 
    block->addRule(new fl::MamdaniRule("if Energy is LOW then Health is BAD", engine)); 
    block->addRule(new fl::MamdaniRule("if Energy is MEDIUM then Health is REGULAR", engine)); 
    block->addRule(new fl::MamdaniRule("if Energy is HIGH then Health is GOOD", engine)); 
    engine.addRuleBlock(block); 

    for (fl::flScalar in = 0.0; in < 1.1; in += 0.1) { 
     energy->setInput(in); 
     engine.process(); 
     fl::flScalar out = health->output().defuzzify(); 
     (void)out; //Just to avoid warning when building 
     FL_LOG("Energy=" << in); 
     FL_LOG("Energy is " << energy->fuzzify(in)); 
     FL_LOG("Health=" << out); 
     FL_LOG("Health is " << health->fuzzify(out)); 
     FL_LOG("--"); 
    } 
} 

回答

1

這基本上是不可能的回答你的問題給出提供的信息。你的問題是關於Bingo類的cleanup方法,但賓果(在堆棧或堆上)的實例無處出現在代碼片段中。同樣,你聲明你正在清理一個「包裝的CPP實例」,但它在其他地方沒有被引用。它確實看起來是你的Test::SimplMamdani方法泄漏 - 你new那裏的一堆對象沒有[至少在泄露的代碼]有任何相應的delete s。同樣,在您的​​方法中,您在堆上創建了一個Test實例,但沒有相應的delete。我假設你的C++代碼中沒有autoptr的內容。

更新,以提供額外的信息:

基於您的意見,您需要檢查C的基本語言結構++。基本規則是你需要delete任何你new。清理Bingo類應該在析構函數(Objective-C的dealloc的C++構造)中執行。你Bingo類看起來應該更像:

Bingo.h:

namespace fl { 
    class Bingo { 
    public: 

     Bingo(); 
     virtual ~Bingo(); 

     void Fuzz(); 
     void setInput(float input); 

     FuzzyEngine* engine; 
     OutputLVar* health; 
     InputLVar* energy; 

    protected: 
    private: 
    }; 
} 

Bingo.cpp:

using namespace fl; 

Bingo::Bingo() { 
} 

Bingo::~Bingo() { 
    if (engine) { 
     delete engine; 
    } 
    if (health) { 
     delete health; 
    } 
    if (energy) { 
     delete energy; 
    } 
} 

當你delete賓果情況下,析構函數將被調用,Bingo「會員變量將被處置。

可以說你的成員變量(引擎,健康和能量)應該是私有的,並通過公共範圍的getter和setter暴露。

拿一份Bjarne Stroustrup的C++參考資料並快速瀏覽一下,或者使用像this one這樣的在線啓動指南。

+0

感謝回覆+對不完整的代碼抱歉。我想問的是,如果我新建一個類賓果實例,然後創建其他類的一些實例作爲屬性,那麼在刪除賓果實例之前,我需要定義一些「清理」方法來刪除這些屬性。 (即我需要清理我**新** ** - 內部賓果屬性不會被釋放否則)。 –

+0

我在我的答案中增加了一些指導信息。 – jstevenco

+0

再次感謝!我知道了。 –