class 个实例的 C++/ 向量作为另一个 class 的 属性

C++/ vector of class instances as property of another class

我正在尝试创建具有私有 属性 _testItems 的 class (PgWeightTestItemSet)。 _testItems 是另一个 class (PgWeightTestItem) 实例的 std::vector。

Header for PgWeightTestItem 包含在 PgWeightTestItemSet.h.

我在 Arduino 中进行代码检查后 IDE 出现错误:

error: 'PgWeightTestItem' was not declared in this scope std::vector <PgWeightTestItem> _testItems;

所以我的问题是:

代码如下:

PgWeightTestItemSet.h

#ifndef PgWeightTestItemSet_h
#define PgWeightTestItemSet_h

#include "Arduino.h"
#include <PgWeightTestItem.h>

class PgWeightTestItemSet {

public:
    PgWeightTestItemSet(std::vector<float> & referenceAv, std::vector<float> & referenceMin, std::vector<float> & referenceMax, bool cumulative);

    /*other public properties and methods*/

private:       
    std::vector <PgWeightTestItem> _testItems;

    /*other private properties and methods*/
};

#endif

PgWeightTestItem.h

#ifndef PgWeightTestItem_h
#define PgWeightTestItem_h

#include "Arduino.h"

class PgWeightStandards {

public:
    PgWeightStandards(float & referenceAv, float & referenceMin, float & referenceMax);

    /*public properties*/

private:
    /*private properties*/
};

#endif

Arduino_project.ino

#include <PgWeightTestItemSet.h>
//other arduino code bellow

目录结构

/root/
/root/Projects/ArduinoProject/Arduino_project.ino
/root/libraries/PgWeightTestItem/
/root/libraries/PgWeightTestItem/PgWeightTestItem.h
/root/libraries/PgWeightTestItem/PgWeightTestItem.cpp
/root/libraries/PgWeightTestItemSet/
/root/libraries/PgWeightTestItem/PgWeightTestItemSet.h
/root/libraries/PgWeightTestItem/PgWeightTestItem.cpp

您在 PgWeightTestItem.h 中定义的 class 称为 PgWeightStandards 而不是 PgWeightTestItem。通过重命名其中一个,你应该没问题。 PgWeightTestItem.h 可以如下:

#ifndef PgWeightTestItem_h
#define PgWeightTestItem_h

#include "Arduino.h"

class PgWeightTestItem {

public:
    PgWeightTestItem(float & referenceAv, float & referenceMin, float & referenceMax);

    /*public properties*/

private:
    /*private properties*/
};

#endif