C ++ /类实例的矢量作为另一个类的属性

时间:2019-07-16 08:17:05

标签: c++ arduino stdvector

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

PgWeightTestItemSet.h中包含PgWeightTestItem的标题。

我在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

1 个答案:

答案 0 :(得分:1)

您在PgWeightTestItem.h中定义的类称为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