2014-01-22 116 views
1

我创建了1个带有2个类的库。 Class Wave和Class LEDLamps。在第二个类的构造函数中,我试图在没有任何运气的情况下填充第一类对象的数组。Arduino:初始化构造函数中的自定义对象

这是我的真实代码的一些部分。 .h文件:

static const int numberOfWaves = 20; 

class Wave 
{ 
public: 
    Wave(int speed, int blockSize, int ledCount, int lightness,int startCount); // Constructor 

private: 

}; 

// ------------------------------------------------------------------------------------------- // 
class LEDLamps 
{ 
public: 
    LEDLamps(int8_t lampCount, int8_t dataPin, int8_t clockPin); //Constructor 

private: 
    Wave waveArray[numberOfWaves]; 
}; 

.cpp文件

Wave::Wave(int speed, int blockSize, int ledCount, int lightness, int startCount) //Constructor 
{ 
      // Doing some stuff... 
} 

// ------------------------------------------------------------------------------------------- // 
LEDLamps::LEDLamps(int8_t lampCount, int8_t dataPin, int8_t clockPin) //Constructor 
{ 
    int i; 
    for (i = 0; i < numberOfWaves; i++) { 
     waveArray[i] = Wave(10,2,25,150,100); 
    } 
} 

错误消息:

LEDLamps.cpp: In constructor 'LEDLamps::LEDLamps(int8_t, int8_t, int8_t)': 
LEDLamps.cpp:66: error: no matching function for call to 'Wave::Wave()' 
LEDLamps.cpp:14: note: candidates are: Wave::Wave(int, int, int, int, int) 
LEDLamps.h:23: note:     Wave::Wave(const Wave&) 

我从错误消息中的参数错误理解什么,但我要送5的整数和构造函数被定义为接收5个整数?所以我一定是别的我做错了...

回答

2

该错误告诉你到底什么错,没有Wave::Wave()方法。您需要Wave类的默认构造函数才能创建它的数组。如果Wave类包含非平凡数据,您可能还想创建一个复制分配操作符。

的问题是,该数组身体LEDLamps构造运行的,所以之前建造LEDLamps构造体内的阵列完全构建,以及你正在做的是转让(使用自动生成的禁止复制赋值运算符)。


不幸的是,默认的Arduino C++库非常有限,至少当涉及到“标准”C++特性时。有libraries that helps,如果有可能使用这样的库,你也可以使用一个std::vector代替,这样可以让你构建其载体在构造函数初始化列表:

class LEDLamps 
{ 
    ... 
    std::vector<Wave> waveVector; 
}; 

... 

LedLamps::LEDLamps(...) 
    : waveVector(numberOfWaves, Wave(10,2,25,150,100)) 
{ 
} 
相关问题