如何在 Arduino 中创建可索引的类对象集(使用 platformIO)

问题描述 投票:0回答:1

菜鸟(和业余爱好者)业余爱好者的问题。

我有一个类库,我可以通过对这些数组进行索引来从数组和结构中提供数据实例,但我一生都找不到一种简单的方法来索引该类的每个实例。

我要做的是实例化离散实例

#include <myclass.h>

为每个按钮实例化每个类

myclass class0; .. myclass classN;

然后我必须用所有测试的结果手动填充一个数组

bool myTests[] {class0.result(),class1.result(),..,classN.result()};

在我可以在 digitalWrite() 调用输出中使用测试结果之前。

我真的希望能够像数组一样实例化我的方法,并在迭代结果的输出的同时迭代这些方法。

据我所知,Arduino 平台不支持向量,尽管我也不完全确定这就是我正在寻找的“机器人”。

有人知道如何在我的具体细节/初学者水平上做到这一点吗?

将 platformIO 与 Arduino 平台结合使用。

我尝试过实例化为数组,

for(int i = 0; i < N; i++){ myclass myinstance[i]; }
当然,这是一次史诗般的失败。笑就好,我想这不会有用。

尝试访问向量、nup,而不是在 Arduino 平台、IDE 或 PIO 上。

尝试了一些事情,事后看来,这些事情太愚蠢了,无法提及,从我在谷歌搜索时读到的内容来看。

而且,是的,我已经在谷歌上搜索了大约四个小时,而且我确信我对这类事情的了解还不足以让我的搜索词正确。也浇在这个古老的地方,这通常是我的第一个度假胜地。

C++之神们肯定有一个优雅的解决方案吗?也许请引导我去看教程并在我离开那里时踢我的屁股......

干杯 脆脆的

c++ arduino platformio
1个回答
0
投票

你的问题不是很清楚,但是这样的事情是否能给你一个提示或前进的方法?

void myFakePrintFunc(int) {}

// Dummy class with a result() function
// The constructor takes 2 arguments just because
class myClass {
   public:
    myClass(int i, bool notUsed) : m_i{i} {}
    int result() const { return m_i; }

   private:
    int m_i;
};

int main() {
    constexpr int SIZE = 3;

    // Make an array of 3 myClass - you don't _need_ the SIZE here, but if it is there the compiler yells at you if there is a mismatch between the array size and the number of objects in the initializer list
    // The small lists are the constructor arguments (2 to show how it works)
    myClass myClasses[SIZE] = {{1, true}, {2, false}, {3, true}};

    // Use SIZE to make a bool array of the same size - here you need SIZE
    // Then populate it with the results
    bool myResults[SIZE];
    for (int i = 0; i < SIZE; ++i) {
        myResults[i] = myClasses[i].result();
    }

    // Or use the range-for loop, if you don't need to care about indexes
    for (const auto& mc : myClasses) {
        myFakePrintFunc(mc.result());
    }
}

另外,通过 Google 快速搜索,似乎有一个 Vector 库,您可以为 Arduino 安装

但这都是相当基础的 C++,所以我认为您也可以从 阅读一本好的 C++ 书中获益匪浅。

© www.soinside.com 2019 - 2024. All rights reserved.