类实例的C++/向量作为另一个类的属性
C++/ vector of class instances as property of another class
我正在尝试创建具有私有属性_testItems的类(PgWeightTestItemSet)。_testItems是另一个类(PgWeightTestItem)的实例的std::vector。
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 中定义的类称为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
相关文章:
- 如何使用指针从另一个类访问属性
- 让 Redhawk 组件访问另一个组件的属性
- 使用 QGenericMatrix 作为另一个类的属性
- C++:对于继承多个类的类,如何从一个类继承某些属性,从另一个类继承其他属性?
- 类实例的C++/向量作为另一个类的属性
- LNK2019与另一个工作项目具有相同属性的项目上的错误
- 具有另一个类对象的属性的类构造函数
- 只有Arduino的第一个对象在包含在另一个对象的集合/数组中时会丢失其数据属性值
- 如何有效地从该对象中包含的另一个对象访问对象字段/属性
- C++:修改一个类在另一个类中的私有属性
- 对象作为另一个对象c++的属性
- 如何在C++中更改另一个类函数中的对象属性?
- 从另一个函数更改窗体"visible"属性?
- 在具有C++中另一个类类型的类中声明属性
- 我们可以为一个类指定一个属性名称吗?即使该属性是另一个表中的列
- BGL:使用绑定属性来存储另一个顶点的顶点描述符
- 包含"random"值的类的数组属性(通过另一个类的指针访问时)
- 从另一个类C++中获取对象的属性
- 如何创建一个类来保存另一个类的对象数组,如何操作其他类的私有属性
- 作为属性指向另一个类的指针