0

我已经漫步到这里的游泳池的深处。我已经取得了一些不错的进展,但现在只是在挣扎。我正在尝试在 iOS 中使用这个模糊逻辑库:http ://code.google.com/p/fuzzy-lite/

我已经编译好了——我所做的是将 .cpp 和 .h 文件添加到我的项目中,并将我的主 viewController 上的后缀更改为“.mm”。我可以在 viewDidload 中运行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;

}

- 包装的 CPP 类的标头

#include "fuzzylite/FuzzyLite.h"

namespace fl {

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

    void setInput(float input);

};
}

来自 ObjC 包装器:

- (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();

}

测试.h

#ifndef FL_TEST_H
#define FL_TEST_H

namespace fl {

class Test {
public:
    static void SimpleMamdani();

};
}


#endif  /* FL_TEST_H */

测试.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("--");
    }
}
4

1 回答 1

1

鉴于提供的信息,基本上无法回答您的问题。您的问题是关于类的cleanup方法Bingo,但是 Bingo 的实例(在堆栈或堆上)在您的代码摘录中没有出现。同样,您声明您正在清理“包装的 CPP 实例”,但在其他任何地方都没有引用它。看来您的方法确实存在泄漏-那里的一堆对象[至少在显示的代码中]没有任何对应的 s。同样,在您的方法中,您在堆上创建一个实例,而没有相应的. 我假设您的 C++ 代码中没有 autoptr 的东西。Test::SimplMamdaninewdeleteFuzzyLiteIOSViewController::viewDidLoadTestdelete

更新以提供更多信息:

根据您的评论,您需要查看 C++ 的基本语言结构。基本规则是你需要delete任何你想要的东西newBingo应在析构函数(Objective-C 的 C++ 构造)中执行对类的清理dealloc。你的Bingo班级应该看起来更像:

宾果游戏.h:

namespace fl {
    class Bingo {
    public:

        Bingo();
        virtual ~Bingo();

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

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

    protected:
    private:
    };
}

宾果游戏.cpp:

using namespace fl;

Bingo::Bingo() {
}

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

当您delete使用 Bingo 实例时,将调用析构函数并Bingo释放 的成员变量。

可以说,您的成员变量(引擎、健康和能源)在范围内应该是私有的,并通过公共范围的 getter 和 setter 公开。

获取一份 Bjarne Stroustrup 的 C++ 参考资料并快速阅读,或者使用像这样的在线入门指南。

于 2012-01-05T02:03:53.977 回答