簡體   English   中英

C ++ /類實例的矢量作為另一個類的屬性

[英]C++/ vector of class instances as property of another class

我正在嘗試創建具有私有屬性_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

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM